Macro To Run on certain sheets

Hobbit

New Member
Joined
Aug 23, 2018
Messages
8
To the forum

Hi there! Hope you are all well. I have a query about running the same macro over a certain number of worksheets.

The macro exists on some (but not every) worksheets within the workbook. If there are 5 worksheets which fall into that category, I have to enter each one and then run the macro on that sheet.

Is there a way for me to:
- get a macro to cycle through all the worksheets, and
- if the tab name contains a key word, then run the macro?
- if not, skip?

Many many thanks in advance to all!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi there, try this:

Code:
Dim i As Integer
Dim findStr As String
Dim TabCount As Long

TabCount = ThisWorkbook.Worksheets.Count

findStr = “keyword” ‘put keyword to search for

For i = 1 To TabCount
If InStr(Sheets(i).name, findStr) > 0 Then
‘Do macro
End If
Next
 
Upvote 0
Another option
Code:
Sub HideSheets()
   Dim Ws As Worksheet
   
   For Each Ws In Sheets(Array("[COLOR=#ff0000]Pcode[/COLOR]", "[COLOR=#ff0000]MA[/COLOR]", "[COLOR=#ff0000]Fluff[/COLOR]"))
      ' Your code here
   Next Ws
End Sub
Change the values in red to the sheet names you want to run the macro on
 
Upvote 0
Another option, using "Like"

Code:
Sub FindSheets()

Dim ws          As Worksheet
Dim sKeyString  As String

sKeyString = "Sheet"

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "*" & sKeyString & "*" Then
        'run your macro
    Else
    End If
Next ws
    
End Sub

And if the key word you're looking for, in each sheet name, could change, you can add an InputBox at the top of the code, to get the "sKeyString" value dynamically.
 
Last edited:
Upvote 0
I tested out the solutions. I think I must be doing something wrong. It executes the macro perfectly, but on the sheet I am looking at when I run it. Not on the sheets with the defined text.

The original macro does not define the worksheet, only the columns to execute it on. I used to run them per sheet. Do I need to modify this to say run on sheet that it is cycling through?

Many thanks again!
 
Upvote 0
Could you please supply your code, without it it's very difficult to figure out what's wrong.
 
Upvote 0
Hi Hobbit,
I believe the solution I provided does what you had asked in your original post. Have you tried my version? It loops through every worksheet within the workbook, and if the sheet's name contains the text you hardcode @ "sKeyString = "Sheet" ", the code that you need to add at "run your macro", would execute.

As for the 'InputBox' component, you could use something like:
Code:
sKeyString = InputBox("Enter the part of the sheet name you want to search for:", _
    "Enter sheet string")

If you opt to use this, you would replace "sKeyString = "Sheet", with the above line of code, making that a dynamic variable, instead of being hardcoded.
 
Upvote 0
Hi there :)

At the moment, my code is as such:

Code:
Sub FindSheets()

Dim ws          As Worksheet
Dim sKeyString  As String

sKeyString = "Option"

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "*" & sKeyString & "*" Then
            Range("AZ:BA,BG:BH,BJ:BJ,AL:AL").Select
            Selection.Delete Shift:=xlToLeft
    Else
    End If
Next ws
    
End Sub

I was hoping that when I ran FindSheets, it would find the worksheets with the word "Option" in it, and then delete those columns on the worksheets with the word "Option".

At the moment, when I run it, it deletes the columns on the sheet that I have it on, regardless of whether or not "Option" is a word in the tab.

Hobbit
 
Upvote 0
Maybe...

Code:
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "*" & sKeyString & "*" Then
            [COLOR="#FF0000"]ws.[/COLOR]Range("AZ:BA,BG:BH,BJ:BJ,AL:AL").Delete Shift:=xlToLeft
    End If
Next ws

or as you are just doing a single test...

Code:
For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "*" & sKeyString & "*" Then [COLOR="#FF0000"]ws.[/COLOR]Range("AZ:BA,BG:BH,BJ:BJ,AL:AL").Delete Shift:=xlToLeft
Next ws
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
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