Select a sheet with a partial name in vba

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
147
Office Version
  1. 365
Platform
  1. Windows
I'm trying to select a sheet in a workbook with vba, however the sheet name varies slightly.
It always starts with "Non-qualifying-transactions-" followed by several numbers.
How can I select this sheet using just those three words? There will always only be on sheet named like that in the workbook.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
One way:
VBA Code:
Sub SelectSheet()
    
    Dim ws As Worksheet
    
    For Each ws In Worksheets
        If Left(ws.Name, 28) = "Non-qualifying-transactions-" Then
            ws.Activate
            Exit For
        End If
    Next ws
    
End Sub
 
Upvote 0
Solution
VBA Code:
Sub SelectSheet()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
    If sh.Name Like "Non-qualifying-transactions-*" Then
        sh.Select
        Exit For
    End If
Next sh
'rest of code
End Sub
 
Upvote 0
Thanks Joe4 and JoeMo
both work.

I appreciate seeing both ways to brush up on my VBA skills!
 
Upvote 0
You are welcome.
Glad we could help.

As you can see, both solutions use the same concepts and are very similar.
 
Upvote 0

Forum statistics

Threads
1,203,467
Messages
6,055,589
Members
444,800
Latest member
KarenTheManager

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