Find and Replace in VBA

  • Thread starter Thread starter Legacy 93538
  • Start date Start date
L

Legacy 93538

Guest
HI

This is probably an odd question but its a problem i have been trying to solve for a few days and have i come up with nothing.

I have a workbook, which is created using my macro. The macro places foruma in column D, what i need to do is replace there cell reference's in the forumlas with a value.

SO far i have tried having the macro create a list of the cell references and the values (in column L and M) needed to be replaced with and doing a find a replace however this did not work as it replaced every cell ref it found eve if it was inside the value it had already replaced the cell ref with.

I then tried the same but insead of replacing the cell ref with the value i added a section which replaced the cel ref with a subsitute value (Column I) and then did a second find and replace of the subistitue value with the value. However this again did the same thing as before.

I have added the code i am using to do the find and replace on the D column

Code:
Z = WorksheetFunction.CountA(PPCWBSht.Range("L2:L20000"))
 
'Loop to convert Excel refs to InpRefs
For Y = 1 To 2
    For X = 1 To Z
        Application.StatusBar = Y & " of " & 2 & "  :  Replacing cell ref with the CalcID : " & X & " of " & Z
        
        If Y = 1 Then
            FindRef = Range("M" & 1 + X)    'First loop with substitute values
            ReplRef = Range("I" & 1 + X)
        Else
            FindRef = Range("I" & 1 + X)    'Second loop with InpRef values
            ReplRef = Range("L" & 1 + X)
        End If
        
        Set MyRange = PPCWBSht.Range("D:D")
            MyRange.Replace What:=FindRef, Replacement:=ReplRef, LookAt:=xlPart, SearchOrder:=xlByRows, _
                MatchCase:=True, SearchFormat:=True, ReplaceFormat:=True
    Next X
Next Y

I have run out of ideas, Can anyone help me??:confused:

Thanks

Jeskit
<!-- / message --><!-- attachments -->
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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