I thought this was a very simple code designed to put borders around all the active cells in a worksheet (data will always reside in cells(1,1)). But I’m getting the infamous “run-time error 1004” and I’m stumped.
Any help would be appreciated.
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
For me it actually errors on the next line
.Range(myrange).Select. You’ve already defined myrange, and you don’t need, in fact can’t, qualify it.Also,
Dim x, y As Integer, only declaresyas anInteger.xis declared as aVariant. While you’re at it, you should declare them asLongs, which is the native VBA type.Also, avoid using
Selectunless necessary. With that all said, here’s how I’d code it: