Find replace using if statement

blaisjoel

New Member
Joined
Oct 28, 2016
Messages
30
I have this code I use to find and replace cell values. I added an If statement to make sure one cell value does not get replaced based on the offset cell. It is not working. Need to know what I am doing wrong.

Code:
Dim oFindRng As Range
Dim Col1 As String


Col1 = "A"


Columns(Col1).Select


Set oFindRng = Columns(Col1).EntireColumn.Find(What:="65500", LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, After:=ActiveCell)


If[U][I][B] oFindRng[/B][/I][/U].Offset(0, 1).Value = "BEARING FEDERAL MOGUL" Then
ActiveCell.Value = "65500"
End If
Do Until oFindRng Is Nothing
    oFindRng.NumberFormat = "@"
    oFindRng.Value = "065500"
    Set oFindRng = Columns(Col1).FindNext(After:=oFindRng)
Loop

I have tried to replace the underlined code oFindRng by Activecell but this did not work neither.
I tried putting the end if after the loop and that did not work neither.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
See if this is what you're after, although it would be more efficient and much faster to use Autofilter and Specialcells(xlCellTypeVisible)...

Code:
Sub x()
Dim oFindRng As Range, c As Range
Dim Col1 As String
Application.ScreenUpdating = False
Col1 = "A"
Set oFindRng = Columns(Col1).EntireColumn.Find(What:="65500", LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, After:=ActiveCell)
For Each c In oFindRng
    If c.Offset(0, 1).Value = "BEARING FEDERAL MOGUL" Then c = "65500"
Next c
oFindRng.NumberFormat = "@"

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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