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:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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.
 
Upvote 0
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
 
Upvote 0
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. ;)
 
Upvote 0
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
 
Upvote 0
First code should be:
Code:
Sub testmacro()
SelectSheets "Section", ThisWorkbook
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

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