Dynamically Add Sheets to Sheets Array

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
223
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

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
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,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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