I'm trying to get a loop function to refer to a range of cells, rather than typing in the worksheet names individually.
I've got this code that works:
Sub LoopTest()
'
' Macro4 Macro
'
'
' Loop Test
Dim SheetsArray As Variant
Dim i As Integer
SheetsArray = Array("Bun1", "Bun2")
For i = LBound(SheetsArray) To UBound(SheetsArray)
With Worksheets(SheetsArray(i)).Select
Range("A2:G60").Select
Selection.ClearContents
End With
Next i
End Sub
But then this code doesn't when I try and get it to refer to a range instead:
Sub LoopTest()
'
'
' Loop Test
Dim Arr As Variant
Dim i As Integer
Dim rng As Range
Set rng = Worksheets("Names").Range("A2:A3")
Arr = rng
For i = LBound(Arr) To UBound(Arr)
With Worksheets(Arr(i)).Select
Range("A2:G60").Select
Selection.ClearContents
End With
Next i
End Sub
I'm getting "Subscript out of range" error when I try and use it, however the worksheets all exist (the range has the same names as the first code). Debug is highlighting the
WIth Worksheets(Arr(i)).Select
line. Any help would be greatly appreciated!
-Cr1kk0