VBA Search/replace with offset 1 column and offset 2 column

Scorpion Steve

Board Regular
Joined
Jun 5, 2011
Messages
220
Hi all.
I am trying to work out how to nest offset in a code using search replace.
The goal is to find a value from cell A1 (example) and replace the values in the cells next to the cell containing samuel and the cell containing samuel.
A sort of search and remove data tool if you like
So for example:
A1: "samuel" (the search criteria)
Search range is: B1:D400
(for example)
Cell B40 = samuel
C40 = Driver
D40 = year
So, the macro is activated and finds "samuel" in B40 and I would like C40 & D40 replaced with ""
The code I'm using is below: (this is just replacing the cell containing the search criteria with "test".
I would like to nest offset(0,2) & offset(0,3).Value = ""
Sub Macro1()
Range("B1:D400").Select
Selection.Replace What:=Sheets("Sheet1").Range("A1").Value, Replacement:="test", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False




End Sub
Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,

Perhaps try a pattern like this:

Code:
Sub example()


Dim strA    As String
Dim oRng    As Range
Dim rNa     As Range
Dim i       As Long
Dim j       As Long


strA = Range("A1") ' string to search
Set oRng = Range("B1:D400") ' range to search in


i = WorksheetFunction.CountIf(oRng, strA) ' count of string in range
Set rNa = oRng(1, 1)
For j = 1 To i
    Set rNa = oRng.Find( _
                What:=strA, _
                After:=rNa, _
                LookIn:=xlValues, _
                LookAt:=xlWhole, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False)
    
    rNa.Offset(0, 1).Resize(1, 2) = ""
Next j


End Sub
This pattern and other useful uses for loops can be found here:
Excel VBA Loops: Correct/Efficient Uses of Excel Loops. Do, For Each and While Loops
 
Upvote 0
If I understand correctly what you want to do, the Range.Replace method is not going to do it because it doesn't identify the specific cells containing the search string. Try using the Range.Find method instead.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,431
Members
448,961
Latest member
nzskater

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