1

I have rows with different strings of text that contains words that are abbreviated e.g. A1 = "Grw Option", B1 ="Grth Fund", C3 ="Grow Account" rather than "Growth Option", "Growth Fund", "Growth Account".

I have the list of different abbreviations down and what i need to replace them with.

However there are about 20 other words that have up to 5 forms of abbreviations, how i have written out the complete VBA code is extremely long.

I wondering is there a possible way to list multiple strings and replace it with a single string using only a single line of code.

I have used the Find & Replace function to replace each abbreviation of "Growth".

Sub ReplaceAbbr()

Dim ws As Worksheet

    ws.Cells.Replace What:="Grw", Replacement:="Growth", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

    ws.Cells.Replace What:="Grth", Replacement:="Growth", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

    ws.Cells.Replace What:="Grow", Replacement:="Growth", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End Sub

I am looking for a shorter alternative to writing this script.

1
  • You could have 2 arrays, one with what you want to replace, and the other one with the replacements in the same order, and then just loop so you only write it once. Or you could turn the replace block into a function and call it giving the worksheet, what to replace and replacement. Commented May 7, 2019 at 6:07

2 Answers 2

1

Option 1:

Sub Test()

    Dim ws As Worksheet
    Dim arrReplace, arrReplacement

    Set ws = ThisWorkbook.Sheets("SheetName")

    arrReplace = Array("Grw", "Grth", "Grow")
    arrReplacement = Array("Growth", "Growth", "Growth")

    For i = LBound(arrReplace) To UBound(arrReplace)
        ws.Cells.Replace What:=arrReplace(i), Replacement:=arrReplacement(i), LookAt:=xlWhole, SearchOrder _
            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Next i

End Sub

Option 2:

Function Replacement(ws As Worksheet, Replace As String, Replacement As String)

    ws.Cells.Replace What:=Replace, Replacement:=Replacement, LookAt:=xlWhole, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End Function
Private Sub Test2()

    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("SheetName")

    Call Replacement(ws, "Grw", "Growth")

End Sub

In option 1 you could also have somewhere in a sheet both lists in columns and assign the arrays to these columns to make it easier.

In option 2 you could also use loops like in option 1.

Sign up to request clarification or add additional context in comments.

8 Comments

Hey thanks a lot @Damian this is exactly what i am looking for ! However by the time the array gets to the "Grow" replace case there are some which are now "Growth" now and the replacement becomes "Growthth". Is there a way to prevent this.
you have LookAt:=xlWhole so it's impossible what you are saying, Growth <> Grow if you are matching the whole word.
My apologies i had to change the code to LookAt:=xlPart as the cells contained two words rather than just the abbreviation. Edit: Altered Initial Code
@Damian i think is not necessary to loop the second array (arrReplacement) because includes the same string. instead you could use Replacement:="Growth" as fix. Additionally, i think is good idea to declare your variable as variant
@Frankis change the order, put first the "biggest" words and go down from there. That's the only way. and Error 1004, I'm assuming he will have more words to replace but he only listed an example.
|
0
Option Explicit

Sub Test()

    Dim ws As Worksheet
    Dim arrReplace As Variant
    Dim strReplacement As String
    Dim i As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")

    arrReplace = Array("Grw", "Grth", "Grow", "ts", "tes", "tet")

    For i = LBound(arrReplace) To UBound(arrReplace)

        Select Case arrReplace(i)

            Case "Grw", "Grth", "Grow"
                strReplacement = "Growth"
            Case "ts", "tes", "tet"
                strReplacement = "Test"

        End Select

        ws.Cells.Replace What:=arrReplace(i), Replacement:=strReplacement, LookAt:=xlWhole, SearchOrder _
            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

    Next i

End Sub

6 Comments

@Damian take a look in my answer to understand what i mean. i have increase the number of short words and i create a case statement to choose which full should be use in the replacement.
I see what you mean, but imagine he has 100 replacements... It's just not worth. 2 arrays will do the job better and cleaner.
Let s say we have this array: arrReplace = Array("ts", "Grth", "Grow","Grw" , "tes", "tet") and we start looping it. what the next array should be?
arrReplacement = Array("Test", "Growth", "Growth", "Growth", "Test", "Test") I don't see the problem there...
so... imagine to have another 20 other words that have up to 5 forms of abbreviations. you will create 2 arrays with more than 100 elements?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.