Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching through range and replacing content of cells that contain specific string in VBA

Tags:

excel

vba

I'm trying to go through a range of cells which contain strings in an excel spreadsheet. If a particular cell contains a specific string, I'd like to replace the entire cell with that same string. I typed out what I think should work, and yet I'm getting nothing. Any help would be appreciated!

Sub Supportclean()

    Dim c As Range
    Dim celltext As String

    For Each c In Range("E:E")
        If InStr(1, celltext, "horse") > 0 Then
            Range(c) = "horse"
        End If
    Next c
End Sub

Thanks so much!

like image 255
J.H. Avatar asked Feb 01 '26 12:02

J.H.


1 Answers

You can do the whole column at once no need for a loop:

Sub supportclean()
    Range("E:E").Replace "*horse*", "horse"
End Sub
like image 154
Scott Craner Avatar answered Feb 04 '26 05:02

Scott Craner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!