Atw*_*p67 0 excel vba excel-vba
我认为这是一个非常简单的代码,用于在工作表中放置所有活动单元格的边框(数据将始终驻留在单元格(1,1)中).但我得到臭名昭着的"运行时错误1004",我很难过.
任何帮助,将不胜感激.
Sub test()
Dim myrange As Range
Dim x, y As Integer
x = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
MsgBox x
'------------------------------'
y = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
MsgBox y
Set myrange = ActiveSheet.Range(Cells(1, 1), Cells(x, y))
With ActiveSheet '<-- erroring here'
.Range(myrange).Select
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
对我来说,它实际上是下一行的错误.Range(myrange).Select.你已经定义了myrange,你实际上不需要,也不需要它.
此外,Dim x, y As Integer仅声明y为Integer.x被宣布为Variant.当你在它时,你应该声明它们Longs,这是本机VBA类型.
另外,Select除非必要,否则请避免使 总而言之,这就是我编码的方式:
Sub test()
Dim myrange As Range
Dim x As Long, y As Long
x = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
y = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
Set myrange = ActiveSheet.Range(Cells(1, 1), Cells(x, y))
With myrange.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
637 次 |
| 最近记录: |