Improper "syntax" returns var name/value in find-replace macro!

IrishMist1748

Board Regular
Joined
Sep 27, 2010
Messages
131
Hello!

I am using the following macro to auto find and replace multiple items at once:


Code:
Sub FindReplace()
Dim i As Integer
Dim FindStr As String
Dim RepStr As String
For i = 1 To 145
    FindStr = Sheet2.Range("A" & i).Value
    RepStr = Sheet2.Range("B" & i).Value
    
    Worksheets("Sheet1").Range("B:B").Cells.Replace What:=FindStr, Replacement:=" RepStr"
Next i
End Sub
Sheet2 contains my list of 'finds' in column A and 'replaces' in column B. Sheet 1, column B is the target for the find and replacement.

Everything works fine but it is OBVIOUS that I have a problem with my syntax somewhere because I get some funny output on my replacement. It seems that the "word" 'Rep' and 'RepStr' and being part of the replacement!

Example:
Find: <table x:str="" style="border-collapse: collapse; width: 368pt;" width="490" border="0" cellpadding="0" cellspacing="0"><col style="width: 368pt;" width="490"><tbody><tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt; width: 368pt;" width="490" height="17">14KWHT W/YEL TRIM RD DIA GENT RING D.25TW</td> </tr></tbody></table>Replacement: <table x:str="" style="border-collapse: collapse; width: 368pt;" width="490" border="0" cellpadding="0" cellspacing="0"><col style="width: 368pt;" width="490"><tbody><tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt; width: 368pt;" width="490" height="17">14 Rep Rep RepStrtr RepStrHT RepStr/ RepStr TRIM RD Rep Rep RepStrtr Rep Rep RepStrtr RING D.25T RepStr

Any ideas on what I am doing wrong here?
</td> </tr></tbody></table>
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You've got the RepStr variable in quotes. VBA's treating it as the string " RepStr" instead of the value the RepStr variable is representing. Try:

Rich (BB code):
Worksheets("Sheet1").Range("B:B").Cells.Replace What:=FindStr, Replacement:=RepStr

And if you want to preserve the space before your RepStr, then try:
Rich (BB code):
Worksheets("Sheet1").Range("B:B").Cells.Replace What:=FindStr, Replacement:=" " & RepStr
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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