Find and replace for multiple terms

jeremyr863

New Member
Joined
Aug 18, 2021
Messages
11
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
  3. Mobile
Hello all,

I need to perform a find and replace on multiple values that I'm sure can be coded in excel.

I have a list of address that have certain words that need to be replaced with a '~' symbol and some terms replaced with their abbreviation.
For instance:


Addraddr_2
123 abc street apt 5123 abc ST ~ 5
456 qwe lane unit 5456 qwe LN ~ 5
678 mno boulevard rm 4678 mno BLVD ~4


Here we are replace apt, unit , and rm with the ~ symbol. Later the text to column function is used to eliminate the truncating value.
Also note street, lane, and boulevard have been replaced with ST, LN, and BLVD.

Im wondering if some sort of code can be used to knock out all 6 find and replaces at once.
I have many more terms to add and im thinking your help can aid as a template.

Much 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.
I thought a UDF might be a better approach if you have add'l changes possible. Modified from something I saw some time ago and used for myself.

Code:
Function MultiReplace(strInput As String, rngFind As Range, rngReplace As Range) As String

Dim strTemp As String, strFind As String, strReplace As String, cellFind As Range
Dim lngColFind As Long
Dim lngRowFind As Long
Dim lngRowReplace As Long
Dim lngColReplace As Long

lngColFind = rngFind.Columns.Count
lngRowFind = rngFind.Rows.Count
lngColReplace = rngFind.Columns.Count
lngRowReplace = rngFind.Rows.Count

strTemp = strInput

If Not ((lngColFind = lngColReplace) And (lngRowFind = lngRowReplace)) Then
    MultiReplace = CVErr(xlErrNA)
    Exit Function
End If

For Each cellFind In rngFind

    strFind = cellFind.Value
    strReplace = rngReplace(cellFind.Row - rngFind.Row + 1, cellFind.Column - rngFind.Column + 1).Value
    strTemp = Replace(strTemp, strFind, strReplace)

Next cellFind

MultiReplace = strTemp

End Function

Book2
ABCDE
1AddrUsing UDFOriginalReplacement
2123 abc street apt 5123 abc ST ~ 5StreetST
3456 qwe lane unit 5456 qwe LN ~ 5streetST
4678 mno boulevard rm 4678 mno BLVD ~ 4LaneLN
51 Main Street Suite 101 Main ST ~ 10laneLN
6BoulevardBLVD
7boulevardBLVD
8apt~
9unit~
10rm~
11room~
12Suite~
13suite~
Addresses
Cell Formulas
RangeFormula
B2:B5B2=MultiReplace(A2,D$2:D$13,E$2:E$13)
 
Upvote 0
Solution
According to your example...
VBA Code:
Sub Eliminate()

    Dim vR As Range, vA, vS As Long, vEliminate, vT As String
   
    For Each vR In Range("A2", Cells(Rows.Count, "A").End(xlUp))
        vA = Split(vR, " ")
        vT = vA(2)
        vEliminate = Array("A", "E", "I", "O", "U", "R")
        For vS = 0 To UBound(vEliminate)
            vA(2) = UCase(Replace(UCase(vA(2)), vEliminate(vS), ""))
            vA(3) = ""
        Next vS
        If Len(vT) <= 6 Then vA(2) = Left(vA(2), 2)
        vR.Offset(0, 3) = Join(vA, " ")
    Next vR
   
End Sub
 
Upvote 0
@kweaver
This looks promising however I am not sure how to enable this code. I dont have experience with vba.

As an end results, im looking to have a single, edited cell that can be used to identify addresses.
For example, I would use the 'cleaned' cell with 123 abc ST or 1 Main ST.
 
Upvote 0
got it, i didnt know simply exiting the VBA window would save your code and allow module use.
Thanks!
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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