Using VBA to Find and Replace All Data in a Cell

austinb

New Member
Joined
Feb 24, 2021
Messages
19
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hello All,

I'd like to piggyback off of this question (VBA using input boxes to find and replace) and add a follow-up question. Instead of simply replacing the found text, I'd like to be able to completely overwrite the cell data with the replace data.

Book1
A
1Apple 3456
2Apple 1234
3Orange 1234
4Orange 3456
Sheet1


So if I ran a Find for Apple and a Replace to Orange, it would change cells A1 & A2 to just "Orange" (no numbers)
Similarly, if I ran a Find for 1234 and a Replace to Lemon, it would change cells A2 & A3 to just "Lemon"

Thank you for any advice.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
A few questions:
- How many replacement values will you have?
- Are those replacements static (the same every time), or might you look for different values each time?
- Where is this list of values to find and what to replace them with found?
- Are you just searching column A, or a bigger range (if so, what is it), or the entire sheet?
 
Upvote 0
Great questions, thank you.
A few questions:
- How many replacement values will you have? Answer: Varies each month
- Are those replacements static (the same every time), or might you look for different values each time? Answer: The specific replacement values will be static each month but which ones will be used will vary each month
- Where is this list of values to find and what to replace them with found? Answer: There won't be a list of values to find because they will vary each month
- Are you just searching column A, or a bigger range (if so, what is it), or the entire sheet? Answer: Will just be values in Column C

Here's some of the actual sample data.

UMB Transactions.xlsm
C
2#41 Ocean Prime Tampa
34 Wheel Parts # 247
45guys 0567 Qsr
576 - Panjabi Corp
676 - Quik Chek 76
776 - United Pacific 5624
87-Eleven 27438
97-Eleven 29938
108884156442logicmonitor
11Ace Hardware 2
12Advance Auto Parts #1058
13Advance Auto Parts #8615
14Advance Auto Parts #8866
15Agent Fee
16Alaska Air
17Alberts Mexican Food 4
18Allegnt*air Bnfdmx
19Allegnt*air Bnz4d7
20Allegnt*air Bphrlk
21Amazon.Com*1b3r08u42 Amzn
22Amazon.Com*1b79i17l2 Amzn
23Amazon.Com*be7rg29h3 Amzn
24Amazon.Com*u02yx5dz3 Amzn
25Amazon.Com*y14d39vx3 Amzn
26American Air
27Amzn Mktp Us
28Apple.Com/Bill
29Applebees 808765180879
30Applebees Ever11211331
31Arbys 5009033
UMB Transactions


To give a little more context, the data is all of the credit card transactions for our company each month. So there might be a charge from McDonald's 12345, McDonald's 77753, McDonald's 00098. So right now I have to go in and either copy/paste "McDonald's" into each of those cells or delete the extraneous data from each cell. We have 30+ cardholders so the instances of similarly named vendors is quite high and manual changes are tedious and time consuming.
 
Upvote 0
To give a little more context, the data is all of the credit card transactions for our company each month. So there might be a charge from McDonald's 12345, McDonald's 77753, McDonald's 00098. So right now I have to go in and either copy/paste "McDonald's" into each of those cells or delete the extraneous data from each cell. We have 30+ cardholders so the instances of similarly named vendors is quite high and manual changes are tedious and time consuming.
To what end do you need to remove this extraneous information?
Are you trying to do some sort of calculation, and this extra information is causing issues (if so, there may be other ways around it without having the mess with the data)?

If we indeed go the VBA route to do this Search/Replace, since the values may change each month, how exactly do you envision communicating with the VBA code which values need to be replaced?
You don't plan on manually updating the VBA each month, do you?
 
Upvote 0
To what end do you need to remove this extraneous information?
Are you trying to do some sort of calculation, and this extra information is causing issues (if so, there may be other ways around it without having the mess with the data)? Answer: To import the data into our accounting software. I already have code that provides the internalid based on the vendor name but it only recognizes an exact match.

If we indeed go the VBA route to do this Search/Replace, since the values may change each month, how exactly do you envision communicating with the VBA code which values need to be replaced? Answer: By using the find and replace message boxes that are detailed in the link from my original post. tl;dr Message box 1 brings up an input field of what to search for and then Message box 2 is to tell it what to replace it with.
You don't plan on manually updating the VBA each month, do you? Answer: Definitely not

Sample data after vendor name change

UMB Transactions.xlsm
CE
1SupplierSupplier Internal ID
2Misc Credit Card Vendor86079
3Misc Credit Card Vendor86079
4Five Guys Burgers & Fries74936
5Misc Credit Card Fuel Vendor75023
6Misc Credit Card Fuel Vendor75023
7Misc Credit Card Fuel Vendor75023
87-Eleven74818
97-Eleven74818
10Logicmonitor Inc95639
11Ace Hardware78966
12Advance Auto Parts74826
13Advance Auto Parts74826
14Advance Auto Parts74826
15Agent Fee1092174
16Alaska Airlines74830
17Misc Credit Card Restaurant Vendor75025
18Allegiant Airlines99343
19Allegiant Airlines99343
20Allegiant Airlines99343
21Amazon.com99408
22Amazon.com99408
23Amazon.com99408
24Amazon.com99408
25Amazon.com99408
26American Airlines74835
27Amazon.com99408
28Apple79003
29Applebees1316786
30Applebees1316786
31Arby's74838
UMB Transactions
Cell Formulas
RangeFormula
E2:E31E2=INDEX(AmEx_Template,MATCH([@Supplier],SearchTable,),1)
Named Ranges
NameRefers ToCells
AmEx_Template='UMB Transactions'!$M$1:$M$4074E2:E31
SearchTable='UMB Transactions'!$N:$NE2:E31
 
Upvote 0
So, you envision code that is going to prompt you for the values to Find/Replace each time?

You mention a list of 30 vendors.
Wouldn't it be better to either store that list on a sheet or in an array some where, and loop through, saying Find/Replace any value that begins with the value with just that value?

So, if "McDonalds" was on that list, it would replace anything that starts with "McDonalds..." with just "McDonalds".
Wouldn't that be better than being prompted 30 times, for each value you want to replace?

If so, do you prefer to store that in a list on a sheet, or in an array directly in VBA?
 
Upvote 0
So, you envision code that is going to prompt you for the values to Find/Replace each time?

You mention a list of 30 vendors.
Wouldn't it be better to either store that list on a sheet or in an array some where, and loop through, saying Find/Replace any value that begins with the value with just that value?

So, if "McDonalds" was on that list, it would replace anything that starts with "McDonalds..." with just "McDonalds".
Wouldn't that be better than being prompted 30 times, for each value you want to replace?

If so, do you prefer to store that in a list on a sheet, or in an array directly in VBA?
Yes, that's what I was envisioning but I'm definitely open to a more efficient solution.

I actually already have all of the possible replacement values in column N so maybe provide me with the code and I can see if I can make it work?
 
Upvote 0
OK, see if this works for you:
VBA Code:
Sub MyReplace()

    Dim lr As Long
    Dim r As Long
    Dim fnd As String
    
'   Find last row in column N with data
    lr = Cells(Rows.Count, "N").End(xlUp).Row
    
'   Loop all rows in column N, starting with row 2
    For r = 2 To lr
'       Get value to replace from column N
        fnd = Cells(r, "N")
'       Ignore "Not found" errors
        On Error Resume Next
'       Do all replacements
        Columns("C:C").Replace What:=fnd & "*", Replacement:=fnd, LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
'       Turn error messaging back on
        On Error GoTo 0
    Next r
    
    MsgBox "Replacements completed"
    
End Sub

I ran it on a sampling of values to replace in column N that look like this:
1646156060932.png

and it worked.
 
Upvote 0
Looks like that works great for the cells that have the extraneous data, so thank you for that! However, it doesn't solve my issue for vendors such as:

American Air (s/b American Airlines)
Caseys ###### (s/b Casey's General Store)
Att (s/b AT&T)
Alaska Air (s/b Alaska Airlines)
etc

Suggestions for those instances?
 
Upvote 0
OK, you didn't mention that some of those replacements are going to be something entirely different (these are the important details that we need to know!)
There is no way that the code can "guess" what you want the replacement to be.

So I think you will need to make a second column, indicating what you want to replace it with, i.e.

1646157123739.png


Then, the code would be updated to:
VBA Code:
Sub MyReplace()

    Dim lr As Long
    Dim r As Long
    Dim fnd As String
    Dim rpl As String
    
'   Find last row in column N with data
    lr = Cells(Rows.Count, "N").End(xlUp).Row
    
    Application.ScreenUpdating = False
    
'   Loop all rows in column N, starting with row 2
    For r = 2 To lr
'       Get value to find from column N
        fnd = Cells(r, "N")
'       Get value to replace it with from column O
        rpl = Cells(r, "O")
'       Ignore "Not found" errors
        On Error Resume Next
'       Do all replacements
        Columns("C:C").Replace What:=fnd & "*", Replacement:=rpl, LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
'       Turn error messaging back on
        On Error GoTo 0
    Next r
    
    Application.ScreenUpdating = True
    
    MsgBox "Replacements completed"
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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