Find/Replace Macro

zoomwaffles

New Member
Joined
Sep 12, 2014
Messages
4
I'm trying to create a macro that searches for the contents of one cell and replaces it with the contents of another cell, and then repeats with two different cells.

I have "WK51" in cell K2, and "WK52" in cell K3... that's the first replacement. Then I have "WK 51" in cell L2, and "WK 52" in cell L3... that's the second replacement.

I have the code below, and it works for the first replacement, but does not execute the second replacement. What am I doing wrong?

Code:
Sub Update_Week_Number()

Dim Findtext As StringDim Replacetext As String
Findtext = Range("K2").Value
Replacetext = Range("K3").Value
Findtext = Range("L2").Value
Replacetext = Range("L3").Value
Cells.Replace What:=Findtext, replacement:=Replacetext, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False


End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Does this solve the problem?

Code:
Sub Update_Week_Number()

Dim Findtext As StringDim Replacetext As String
Findtext = Range("K2").Value
Replacetext = Range("K3").Value
Cells.Replace What:=Findtext, replacement:=Replacetext, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Findtext = Range("L2").Value
Replacetext = Range("L3").Value
Cells.Replace What:=Findtext, replacement:=Replacetext, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False




End Sub

(this is probably not the most efficient way to do this...)

you have to execute the replace command for each value in Findtext and Replacetext
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,674
Members
448,977
Latest member
moonlight6

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