2

I was wondering if there is a way to reference multiple sheets in a single line in Excel VBA. I know you are able to reference multiple ranges, columns, rows via:

Range("G1:G5, G6:G10, H5:H10") etc...

I want to be able to do the same thing with sheets I tried this:

Sheets("Sheet1_Name, Sheet2_Name, Sheet3_Name").Range(...)

But that didn’t work so I was wondering if there was a special way to do this aside from putting the sheet names into variables then referencing the variables.

Ry-
  • 218,210
  • 55
  • 464
  • 476
user1305569
  • 81
  • 3
  • 6
  • 13

1 Answers1

3
Sub Test()
    Dim sheetsArray As Sheets
    Set sheetsArray = ActiveWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3"))

    Dim target As Range
    Dim sheetObject As Worksheet

    ' change value of range 'a1' on each sheet from sheetsArray
    For Each sheetObject In sheetsArray
        Set target = sheetObject.Range("A1")
        target.Value = "Test"
    Next sheetObject
End Sub
Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51