Multi find and replace part of cell

tigerzen

Board Regular
Joined
Mar 8, 2023
Messages
209
Office Version
  1. 365
Platform
  1. Windows
Trying to make replacements to raw data which comes in all sorts of variations eg Diploma could be dip, DP, Dipl. I've tried a macro but it doesn't do anything and that may be because I'm using a work computer. Looking for a formula to help convert the raw data with what's in the accompanying table. To be on the safe side, I'll need the formula to work on Office 2019 as I have a spare computer with that on it. If there's an obvious problem with my macro, then I'll fix it but I've used macros like these before and they've been fine so a formula/function will probably be how I go on this one. If there's an easier way to do it on Office 365, I'll look at that too. Open to suggestions here because I spend a lot of time cleaning data which is inconsistently entered.

Multireplace macro.xlsm
ABCDEF
1Should readTable for macro
2CourseCourseOldNew
3Adv Dip ScienceAdvanced Diploma ScienceAdvAdvanced
4As Deg BusinessAssociate Degree BusinessDipDiploma
5D BusDiploma BusinessD Diploma
6Cer MathsCertificate MathsCerCertificate
Sheet1


The macro is

Sub MultiReplace()

Dim ListItem As Range
Dim ListToReplaceWithin As Range
Dim ListOfThingsThatWillChange As Range

On Error GoTo ErrorHandler

Set ListToReplaceWithin = Application.InputBox(Prompt:="Select the list you want to replace within:", Title:="Replace Items", Type:=8)
Set ListOfThingsThatWillChange = Application.InputBox(Prompt:="Select the list of items that you want to change:", Title:="Replace With", Type:=8)

For Each ListItem In ListOfThingsThatWillChange
ListToReplaceWithin.Replace What:=ListItem, Replacement:=ListItem.Offset(0, 1)
Next ListItem

Exit Sub

ErrorHandler:
Exit Sub

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Can you try changing the replace line to this:
VBA Code:
ListToReplaceWithin.Replace What:=ListItem, Replacement:=ListItem.Offset(0, 1), LookAt:=xlPart, MatchCase:=False
 
Upvote 0
Can you try changing the replace line to this:
VBA Code:
ListToReplaceWithin.Replace What:=ListItem, Replacement:=ListItem.Offset(0, 1), LookAt:=xlPart, MatchCase:=False
Thanks Alex, that showed that macros can run on this machine. It has partially worked but possible macros aren't the way to go here. I got some funny results though, the output was:

AdvanceDiploma Diploma Science
Associate Degree Business
Diploma Bus
Certificate Maths
Diploma Sci
 
Upvote 0
Better to check each word.
try change to
Code:
Sub MultiReplace()

    Dim ListItem As Range
    Dim ListToReplaceWithin As Range
    Dim ListOfThingsThatWillChange As Range, x, myList, i&, ii&
    
    On Error GoTo ErrorHandler
    
    Set ListToReplaceWithin = Application.InputBox(Prompt:="Select the list you want to replace within:", Title:="Replace Items", Type:=8)
    Set ListOfThingsThatWillChange = Application.InputBox(Prompt:="Select the list of items that you want to change:", Title:="Replace With", Type:=8)
    myList = Application.Trim(ListToReplaceWithin.Resize(, 2))
    For Each ListItem In ListOfThingsThatWillChange
        x = Split(ListItem)
        For i = 0 To UBound(x)
            For ii = 1 To UBound(myList, 1)
                If x(i) = myList(ii, 1) Then x(i) = myList(ii, 2)
            Next
            ListItem = Join(x)
        Next
    Next ListItem
    
    Exit Sub
    
ErrorHandler:
    Exit Sub

End Sub
 
Upvote 0
Better to check each word.
try change to
Code:
Sub MultiReplace()

    Dim ListItem As Range
    Dim ListToReplaceWithin As Range
    Dim ListOfThingsThatWillChange As Range, x, myList, i&, ii&
   
    On Error GoTo ErrorHandler
   
    Set ListToReplaceWithin = Application.InputBox(Prompt:="Select the list you want to replace within:", Title:="Replace Items", Type:=8)
    Set ListOfThingsThatWillChange = Application.InputBox(Prompt:="Select the list of items that you want to change:", Title:="Replace With", Type:=8)
    myList = Application.Trim(ListToReplaceWithin.Resize(, 2))
    For Each ListItem In ListOfThingsThatWillChange
        x = Split(ListItem)
        For i = 0 To UBound(x)
            For ii = 1 To UBound(myList, 1)
                If x(i) = myList(ii, 1) Then x(i) = myList(ii, 2)
            Next
            ListItem = Join(x)
        Next
    Next ListItem
   
    Exit Sub
   
ErrorHandler:
    Exit Sub

End Sub
Thanks Fuji, I gave this a try and pretty much did the same thing as in Post 3
 
Upvote 0
Did you add missing data like this?
Book1
ABCDEFG
1Should readTable for macro
2CourseCourseOldNew
3Advanced Diploma ScienceAdvanced Diploma ScienceAdvAdvanced
4Associated Degree BusinessAssociate Degree BusinessDipDiploma
5Diploma BusinessDiploma BusinessD Diploma
6Certificate MathsCertificate MathsCerCertificate
7AsAssociated
8BusBusiness
9DegDegree
10
11
Sheet1
 
Upvote 0
Did you add missing data like this?
Book1
ABCDEFG
1Should readTable for macro
2CourseCourseOldNew
3Advanced Diploma ScienceAdvanced Diploma ScienceAdvAdvanced
4Associated Degree BusinessAssociate Degree BusinessDipDiploma
5Diploma BusinessDiploma BusinessD Diploma
6Certificate MathsCertificate MathsCerCertificate
7AsAssociated
8BusBusiness
9DegDegree
10
11
Sheet1
Hi Fuji, yes I made those changes but still got the same result.
 
Upvote 0
Strange,

Col.A in my previous post is the result of the code I posted...
 
Upvote 0
Strange,

Col.A in my previous post is the result of the code I posted...
I could well be doing something wrong at my end, I'll work on it later today and see, thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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