VBA Macro Select Worksheet If Name Contains String

Poisson

New Member
Joined
Sep 28, 2020
Messages
3
Office Version
  1. 2007
Platform
  1. Windows
Complete macro noob and not sure what to do. I have borrowed a macro from this forum to select a tab based on the worksheet name

VBA Code:
Sub Macro1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "* Apple" Then
        ws.Select
        Exit For
    End If
Next ws
End Sub

It is able to search for the Apple string.

However, how do I modify this so that it allows for "Pear" as well? I tried the following:

Code:
If ws.Name Like "* Apple" Or "* Pear" Then

and ended up with a debug error (runtime 13 - type mismatch)

Is there a way to stick the "Apple" or "Pear" search together? I just want to select the worksheet if it contains either string.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
VBA Code:
Sub Macro1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "* Apple" Or ws.Name Like "* Pear" Then
        ws.Select
        Exit For
    End If
Next ws

End Sub
 
Upvote 0
Good Day

what about this, Feedback is highly appreciated with (y)?

VBA Code:
Sub Macro1()
Dim WB As Workbook: Set WB = ThisWorkbook
Dim WS As Worksheet
N = WB.Worksheets.Count

Dim Names As String
 
 For Each WS In WB.Worksheets
    If WS.Name Like "*Apple*" Or WS.Name Like "*Pear*" Then
 
 
      Names = Names & IIf(Names <> "", ",", "") & WS.Name
      
    End If
Next WS
Worksheets(Split(Names, ",")).Select


End Sub


Complete macro noob and not sure what to do. I have borrowed a macro from this forum to select a tab based on the worksheet name

VBA Code:
Sub Macro1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "* Apple" Then
        ws.Select
        Exit For
    End If
Next ws
End Sub

It is able to search for the Apple string.

However, how do I modify this so that it allows for "Pear" as well? I tried the following:

Code:
If ws.Name Like "* Apple" Or "* Pear" Then

and ended up with a debug error (runtime 13 - type mismatch)

Is there a way to stick the "Apple" or "Pear" search together? I just want to select the worksheet if it contains either string.
 
Upvote 0
Stick together

SelectGif.gif
 
Upvote 0
Fantastic - Thanks all for such quick replies!!!

I am testing it out now and will report back
 
Upvote 0
All solutions worked! I went with Dossfm0q's solution but everyone's angle worked.

Thanks all!
 
Upvote 0
Thank you too much for your feedback, and I am glad to help you and your rate that support me
 
Upvote 0

Forum statistics

Threads
1,214,866
Messages
6,121,996
Members
449,060
Latest member
mtsheetz

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