VBA - Search text and Replace string in another cell

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I tried searching but could not find the answer, perhaps Im not using the correct search terms.

Im trying to search in 1 column and if its finds string or part of a string, then it would replace another cells values, in the same row.

Example,
Under Job column, C3 and C6 both contains 234, then I want B3 and B6 to be changed to Yes.
The number of rows will vary.

Book1
ABC
1NamePlaceJob
2testzzz123
3testzzz234
4testzzz345
5testzzz123
6testzzz234
7testzzz345
Sheet1
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Copy this formula down the B column
= IF(C2=234,"Yes","zzz")
 
Upvote 0
Copy this formula down the B column
= IF(C2=234,"Yes","zzz")
Hi thanks for the reply.

I should have been clear, the Name and Place columns I used were just fillers.. they would be actual names and places.
 
Upvote 0
How about:

VBA Code:
Sub MatchReplace()
    Dim ArrayValue      As Long
    Dim LastRow         As Long
    Dim Rng             As Range
    Dim FirstAddress    As String
    Dim MyArr           As Variant
'
    MyArr = Array("234")                                                        ' Strings to search for in the C column
'
    With Sheets("Sheet1")
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
'
    With Sheets("Sheet1").Range("C2:C" & LastRow)
        For ArrayValue = LBound(MyArr) To UBound(MyArr)
            Set Rng = .Find(What:=MyArr(ArrayValue), After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
            If Not Rng Is Nothing Then                                          ' If match found then
                FirstAddress = Rng.Address
'
                Do
                    Rng.Offset(0, -1).Value = "Yes"                             '   Place our string in the other cell
                    Set Rng = .FindNext(Rng)
                Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
            End If
        Next ArrayValue
    End With
End Sub
 
Upvote 0
Solution
Thank you so much.. this works well.
Thank you for the quick response. :)

Just an FYI, you can add more strings to test for in the line:

VBA Code:
    MyArr = Array("234")                                                        ' Strings to search for in the C column

For example, if you wanted to check for "234" & "345", you could edit that line of code to:

VBA Code:
    MyArr = Array("234", "345")                                                       ' Strings to search for in the C column

In that case, all of the "234"'s would be marked "Yes" and all of the "345"'s would also be marked "Yes"

@gd6noob Thank you for reporting back your status!
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

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