Select sheets based on name

theta

Well-known Member
Joined
Jun 9, 2009
Messages
960
Hi,

I need a VBA module or code that will find all sheets in the current workbook that contain the word "Section"

Then pass them into an array and select them

Sheets(Array("First Sheet", "Second Sheet")).Select

From here I will perform some actions but this is one part I am struggling with

Thinking of writing it like this, but need help forming the array element to store each sheet name that meets the instr criteria (or should I be using LIKE ?)

Code:
Public Function SelectSheets(sht as String, Optional wbk as Workbook)
 
Dim ws as Worksheet
 
For each ws in wbk.sheets
 
If InStr(1,ws.name,sht)

Then from here I need a logic test, if >0 then add the ws.name to an array...or redim an existing array. So eventually will have an array full of sheet name that meet the sht criteria...then this can be passed to the Sheets(Array()).Select statement?
 
Last edited:

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Code:
Public Sub SelectSheets(sht As String, Optional wbk As Workbook)
   Dim blnFirst As Boolean
   If wbk Is Nothing Then Set wbk = ActiveWorkbook
   Dim ws As Object
   blnFirst = True
   For Each ws In wbk.Sheets

      If InStr(1, ws.Name, sht, vbTextCompare) > 0 Then
         ws.Select blnFirst
         blnFirst = False
      End If
   Next ws
End Sub
for example.
 

theta

Well-known Member
Joined
Jun 9, 2009
Messages
960
Hi Rorya,

Could you explain the logic behind this

I'm guessing this goes through each workbook and sets the select property to true (if criteria is met)

Is this more efficient than passing to an array?

Also, why is ws an Object and not Dim as a sheet?

thanks!

VB is coming on ok...I know the limits of excel so can map out an entire logical process...its just a case of getting the syntax correct
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
It loops through and selects each sheet if it matches the criteria. (the optional boolean argument for select is used to override any existing selection when the first sheet is selected but to add to the existing selection for all subsequent sheet)
there is no Sheet object which is why I used Object. ;)
 

theta

Well-known Member
Joined
Jun 9, 2009
Messages
960

ADVERTISEMENT

Hi Rorya,

I had a play and am using a bit of your code along with some arrays

But I get a compile error ("=" expected)

Something obvious but I can't spot it :

Code:
Sub testmacro()
SelectSheets("Section", ThisWorkbook)
End Sub
Public Sub SelectSheets(sht As String, Optional wbk As Workbook)
     
    Dim wks As Worksheet
    Dim ArrWks() As String
    Dim i As Long
    
    If wbk Is Nothing Then Set wbk = ActiveWorkbook
    
    ReDim ArrWks(0 To Worksheets.Count - 1)
    For Each wks In Worksheets
        If InStr(1, wks.Name, sht) > 0 Then
            ArrWks(i) = wks.Name
            i = i + 1
        End If
    Next wks
    ReDim Preserve ArrWks(i - 1)
    Sheets(ArrWks).Select
End Sub
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
First code should be:
Code:
Sub testmacro()
SelectSheets "Section", ThisWorkbook
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,509
Messages
5,596,568
Members
414,079
Latest member
Frills

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
Top