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.
 

Some videos you may like

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Nothnless

Board Regular
Joined
Apr 28, 2016
Messages
134
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
 

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
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.
 

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

Stick together

SelectGif.gif
 

Poisson

New Member
Joined
Sep 28, 2020
Messages
3
Office Version
  1. 2007
Platform
  1. Windows
Fantastic - Thanks all for such quick replies!!!

I am testing it out now and will report back
 

Poisson

New Member
Joined
Sep 28, 2020
Messages
3
Office Version
  1. 2007
Platform
  1. Windows
All solutions worked! I went with Dossfm0q's solution but everyone's angle worked.

Thanks all!
 

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
Thank you too much for your feedback, and I am glad to help you and your rate that support me
 

Watch MrExcel Video

Forum statistics

Threads
1,122,519
Messages
5,596,635
Members
414,083
Latest member
Mrsash

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