How to reference multiple sheets?

bradlydraper

New Member
Joined
May 13, 2015
Messages
26
I have pretty much a giant dictionary within my code, which recognizes common acronyms and turns them into spelled out words.


Take for example
Code:
Dim Ws As Worksheet
Set Ws = Worksheets("Sheet 2")

With ws
Cells.Replace What:=" m ", Replacement:="male", LookAt:=xlPart, SearchOrder:= xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:=" f ", Replacement:="female", LookAt:=xlPart, SearchOrder:= xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End With

I have about 1200 lines of this, so taking the route of just writing the sheet at the start of the line is not a route that I would like to take


So what I want it to do now is for this to work on multiple worksheets. As you can see with the code above, it only changes these words for Sheet 2. I want it to change the words for Sheet 3 as well. I've tried things like

Code:
Dim sh2 as Worksheet
Dim sh3 as Worksheet

Set sh2 = ActiveWorkbook.Sheets("Sheet 2")
Set sh3 = ActiveWorkbook.Sheets("Sheet 3")

With Sh2 and Sh3
""
End With

And I've also tried just typing "With Sheets("Sheet 2") And ("Sheet 3") at the top, but I'm definitely not an expert with this and can't seem to figure it out

Any help is really appreciated
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You can try something like this:

Code:
Dim ws As Worksheet
For Each ws In Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6"))
	
With ws
	Cells.Replace What:=" m ", Replacement:="male", LookAt:=xlPart, SearchOrder:= xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
	Cells.Replace What:=" f ", Replacement:="female", LookAt:=xlPart, SearchOrder:= xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End With

Next ws
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,031
Members
448,940
Latest member
mdusw

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