Find Replace Cell Value Dynamic Range

The Chad

New Member
Joined
Oct 17, 2011
Messages
26
Hi all,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
I have been working on a macro to find either “0” or “#N/A” and replace with “”. The 0 or #N/A is the result of a vlookup where I have done a paste special to retain the value.
<o:p> </o:p>
Code that is working (modified from unknown person)
Code:
[/SIZE][/FONT]
[SIZE=3][FONT=Times New Roman]Sub FindReplaceABCD()<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Dim SFind1<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Dim SFind2<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Dim SReplace<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    SFind1 = "0"<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    SFind2 = "#N/A"<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    SReplace = ""<o:p></o:p>[/FONT][/SIZE]
<o:p>[FONT=Times New Roman][SIZE=3] [/SIZE][/FONT]</o:p>
[SIZE=3][FONT=Times New Roman]    Range("J2:BC1150").Replace _<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]        What:=SFind1, Replacement:=SReplace, _<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    LookAt:=xlWhole, MatchCase:=False<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Range("J2:BC1150").Replace _<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]        What:=SFind2, Replacement:=SReplace, _<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    LookAt:=xlWhole, MatchCase:=False<o:p></o:p>[/FONT][/SIZE]
<o:p>[FONT=Times New Roman][SIZE=3] [/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]
<o:p> </o:p>
Code that I am working on where I am getting a Run-time error ‘13’ at the If Statement.
Code:
[/SIZE][/FONT]
[SIZE=3][FONT=Times New Roman]Sub FindReplaceabc()<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Dim i, j, lRow, lColumn<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Dim SFind1<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Dim SFind2<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Dim SReplace<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    SFind1 = "0"<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    SFind2 = "#N/A"<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    SReplace = ""<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    'Application.Calculation = xlCalculationManual<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Application.ScreenUpdating = False<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    lRow = Range("A" & Rows.Count).End(xlUp).Row<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    lColumn = ActiveSheet.Cells.Find(What:="*", SearchDirection:=xlPrevious, _<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    SearchOrder:=xlByColumns).Column<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    For j = 10 To lColumn 'column J (10) to last column<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]        For i = lRow To 2 Step -1<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]            <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]            'Error occurring in if statement below "Runtime error 13"<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]            If Cells(i, j).Value = SFind1 Or SFind2 Then<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]                Cells(i, j).Value = SReplace<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]            <o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]            End If<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]        Next i<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Next j<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    Application.ScreenUpdating = True<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]    'Application.Calculation = xlCalculationAutomatic<o:p></o:p>[/FONT][/SIZE]
<o:p>[FONT=Times New Roman][SIZE=3] [/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]
<o:p> </o:p>
I am using Excel 2003 and need the find replace in a macro as it is part of a larger piece of work.
<o:p> </o:p>
Any assistance is appreciated.

cheers,
Chad
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
'Error occurring in if statement below "Runtime error 13"<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
If Cells(i, j).Value = SFind1 Or SFind2 Then[/Code]
That syntax is not correct for an Or test... you must do two separate comparisons inside the If..Then statement. Try it like this...

Code:
If Cells(i, j).Value = SFind1 Or Cells(i, j).Value = SFind2 Then
 
Upvote 0
sensational - thanks Rick:)

I also forgot to mention in my original post that I received assistance from Weaver with the if statement for locating each cell.
 
Upvote 0

Forum statistics

Threads
1,222,174
Messages
6,164,391
Members
451,889
Latest member
invalidlabel

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