VBA Like Operator

danbrawl93

New Member
Joined
Dec 15, 2016
Messages
17
Hello,

I have been using the Like operator in VBA to activate sheets if they have a given start to their name. See example below:

Sheet name = "AI. 1234567"
Identifying start to name = "AI."

The code for this would be as follows:

Code:
For Each sh In Worksheets
If sh.name Like "AI.*" Then
sh.Activate
' Do stuff
End If
Next sh

However, I now have an issue due to a sheet being named in the following format, which is also activated: "MA. Example"

I thought my code above would simply activate sheets beginning, and only beginning, with "AI." with the * providing a wildcard for the rest of the sheet name. Clearly I am mistaken, so could somebody please explain how the * works and why the undesired sheet is being activated? If an alternative can be used to activate sheets with a specific start to its name ("AI." in this case) then please let me know, as this is the desired effect.

Many, many thanks!

Dan :)
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try parsing out the left 3 characters of the sheet name using the LEFT() function and using that as the comparison.

Code:
For Each sh In Worksheets
    If Left(sh.Name, 3) = "AI." Then
        sh.Activate
        ' Do stuff
    End If
Next sh
 
Upvote 0
I thought my code above would simply activate sheets beginning, and only beginning, with "AI." with the * providing a wildcard for the rest of the sheet name.
I would agree with you, and having tested your code it does just that.
An alternative would be
Code:
If Left(sh.Name, 3) = "AI." Then
 
Upvote 0
Just tried the code again and it no longer opens the page in the naming format "MA. Example. Absolutely no idea why it was happening before, just a case of closing and re-opening Excel... Thanks for your alternative suggestions, they make sense :)
 
Upvote 0
Update: My code was opening the active sheet, which was the "MA. Example" one - so I was wrong altogether haha
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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