Dynamically Add Sheets to Sheets Array

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
224
Office Version
  1. 365
Platform
  1. 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
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
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi @wsnyder

You are not explaining why you need all the sheets in an array.

I give you an example:

VBA Code:
Sub SheetsArray()
  Dim sh As Worksheet
  Dim wks()
  Dim n As Long
 
  If Sheets.Count > 1 Then
    For Each sh In Sheets
      ReDim Preserve wks(n)
      wks(n) = sh.Name
      n = n + 1
    Next
    'What do you want to do with the sheet array?
    'For example, copy all sheets:
    Sheets(wks).Copy
  Else
    MsgBox "Only 1 worksheet in the workbook"
  End If
End Sub

But if you want to select all the sheets if they are more than one sheet, just:
VBA Code:
Sub SheetsArray_v2()
  If Sheets.Count > 1 Then
    Sheets.Copy
  Else
    MsgBox "Only 1 worksheet in the workbook"
  End If
End Sub


--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top