Freigeben über


Excel VBA: Replacing text in a specific column with code

Another really quick code snippet to replace text in a specific column through code.

Usage is simple: ReplaceTextInColumn "A", "Hello", "Aloha"
Where "A" is the column to replace text in.
Hello is the text to find, and Aloha is the text to replace Hello with (thinking warm this winter).

You can extend this to add more flexibility to the sub arguments (adding case matching, etc).

 
Sub ReplaceTextInColumn(strColumn, strSource, strDest As String)
Columns("" + strColumn + "").Replace What:="" + strSource + "", _
Replacement:="" + strDest + "", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End Sub

Enjoy!

— Easy link to my blog: https://aka.ms/leesteve
If you like my blogs, please share it on social media and/or leave a comment.