Run Macro on specific sheets

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
168
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I have a macro that does the following:-

VBA Code:
Sub A_RenameSheets()

Dim ws As Worksheet

Path = ThisWorkbook.Path & "\"

On Error GoTo err_chk

For Each ws In Worksheets

If Len(Trim(ws.Range("B4"))) > 0 Then ws.Name = ws.Range("B4").Text

Next ws

On Error GoTo 0

Exit Sub

err_chk:

MsgBox "Error #:" & Err.Number & ": " & Err.Description, vbOKOnly, "ERROR RENAMING " & ws.Name

Err.Clear

Resume Next

End Sub


Rather than running the macro on all the sheets in the workbook, I only want it to run on sheets that start Shee*

I am sure the line For Each ws In Worksheets needs to be changed, but I haven’t got a clue of what to do to just run on specific sheets

Any help would be appreciated.

Thanks
 
Last edited by a moderator:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Just change this line:
VBA Code:
If Len(Trim(ws.Range("B4"))) > 0 Then ws.Name = ws.Range("B4").Text
to this:
VBA Code:
If (Len(Trim(ws.Range("B4"))) > 0) And (Left(ws.Name, 4) = "Shee") Then ws.Name = ws.Range("B4").Text
 
Upvote 0
How about
VBA Code:
For Each ws In Worksheets
   If ws.Name Like "Shee*" Then
      If Len(Trim(ws.Range("B4"))) > 0 Then ws.Name = ws.Range("B4").Text
   End If
Next ws
 
Upvote 0
Solution
Joe4, Fluff,

Thanks for the prompt responses, I will try out both solutions tomorrow and let you know.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,465
Members
448,965
Latest member
grijken

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