wsnyder
Board Regular
- Joined
- Sep 23, 2018
- Messages
- 224
- Office Version
- 365
- Platform
- Windows
Hi all,
I am trying to dynamically add sheets to a sheets array if some condition is met.
How can I accomplish this?
I thought maybe I could build up a string of sheet names, but in the code below I am getting
when trying to pass the string of sheet names to the Sheets Array
Thanks,
-w
I am trying to dynamically add sheets to a sheets array if some condition is met.
How can I accomplish this?
I thought maybe I could build up a string of sheet names, but in the code below I am getting
Subscript out of range error
when trying to pass the string of sheet names to the Sheets Array
Thanks,
-w
VBA Code:
Option Explicit
Sub SheetsArray()
Dim wb As Workbook
Dim ws As Worksheet
Dim wsArr As Sheets
Dim strSheets As String
Dim i As Integer
'Initialize
Set wb = ThisWorkbook
i = wb.Worksheets.Count
strSheets = vbNullString
With wb
If i > 1 Then
For Each ws In .Worksheets
strSheets = strSheets & """" & ws.Name & """" & ","
Next ws
strSheets = Left(strSheets, Len(strSheets) - 1)
Debug.Print strSheets
Set wsArr = .Sheets(Array(strSheets))
Else
MsgBox "Only 1 worksheet in the workbook"
End If
End With
End Sub