Amend formula if WS name like

harveyldj93

New Member
Joined
Aug 23, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a Macro which will correct all the worksheets that contain the phrase Perm, this has a number first i.e 0000 Perm.
The code I have written is below, however it only seems to amend the first tab. Does anyone know why it isn't cycling through?

If ws.Name Like "*Perm" Then
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "*Perm" Then
ActiveSheet.Unprotect "****"
Range("M261:M269").Select
Selection.Replace What:="T", Replacement:="U", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
ActiveSheet.Protect "****"
End If
Next ws
End Sub

Appreciate it!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Welcome to the Board!

Note that looping through the worksheet names does NOT actually select/activate them.
So all range references without a worksheet component only apply to the sheet you are on when you started the macro (that is the "ActiveSheet").

You can either add worksheet references to all of your ranges, or activate the sheet, i.e.
Rich (BB code):
If ws.Name Like "*Perm" Then
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name Like "*Perm" Then
            ws.Activate
            ActiveSheet.Unprotect "****"
            Range("M261:M269").Replace What:="T", Replacement:="U", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
            ActiveSheet.Protect "****"
        End If
    Next ws
End If

End Sub
 
Upvote 0
Solution
Welcome to the Board!

Note that looping through the worksheet names does NOT actually select/activate them.
So all range references without a worksheet component only apply to the sheet you are on when you started the macro (that is the "ActiveSheet").

You can either add worksheet references to all of your ranges, or activate the sheet, i.e.
Rich (BB code):
If ws.Name Like "*Perm" Then
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name Like "*Perm" Then
            ws.Activate
            ActiveSheet.Unprotect "****"
            Range("M261:M269").Replace What:="T", Replacement:="U", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
            ActiveSheet.Protect "****"
        End If
    Next ws
End If

End Sub
Thats perfect! Thanks very much for your help.
 
Upvote 0
You are welcome.

I hope the explanation makes sense.
It is a common mistake that people make, not realizing that looping through the sheet names does not actually activate each sheet.
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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