Home » excel » excel – Can I modify this macro to run just on one column?

excel – Can I modify this macro to run just on one column?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m using this to find and replace values, and it works fine for the entire sheet I have open:

Sub replaceStringInCells()
Dim wTxt As String
Dim rTxt As String
Dim rNum As Integer
rNum = 0
For Each Row In Range("swapvalues").Rows  '<== change the wordlist Name here as required
    wTxt = Row.Cells(1).Value
    rTxt = Row.Cells(2).Value
        Selection.Replace What:=wTxt, Replacement:=rTxt, LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
            rNum = rNum + 1
Next
End Sub

I’d like to edit it so that I runs just on Column U–instead of the full sheet.

I’ve seen other folks try to solve this using lines like Set rng = Range("U:U") but I’m not sure where to add that in the code above or if this is the best approach.

If anyone has a suggestion, I’d appreciate it.

How to&Answers:
 Range("U:U").Replace What:=wTxt, Replacement:=rTxt, LookAt:=xlPart