I want to create an one-dimensional array with Strings and I tried this code:
Dim sheets_names_array() As String = { "Sheet1", "Sheet2" }
but I get a 'Compile error: Syntax error' when trying to run the code.
What is my error? Many thanks, Electra
CodePudding user response:
here is correct example of your approach:
Public Sub PrintArray()
' make an array with the sheet names we want to parse
Dim sheets_names_array() As Variant, sheet As Variant
sheets_names_array = Array("Sheet1", "Sheet2")
' loop the sheets array
For Each sheet In sheets_names_array
Debug.Print (sheet & " ")
Next
End Sub
and this one is usual loop through each sheet as you declare sheet variable anyway:
Sub Test()
' Declare ws as a worksheet object variable.
Dim ws As Worksheet
' Loop through all of the worksheets in the active workbook.
For Each ws In ThisWorkbook.Worksheets
Debug.Print ws.Name
Next
End Sub
P.S. not sure why SO cannot recognise the code this time...