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.
 
I think that's why I was going with the Find and Replace option originally. I've tried to make this process more efficient in the past but have been too busy for the last few months to work on it.

I don't think the new code would work because the vendor names can vary so much. For example, this current month has all of the following names which should be changed to Casey's General Store:

Caseys #2080
Caseys #2587
Caseys #2605
Caseys #3272
Caseys #3272
Caseys #3272
Caseys #3272
Caseys #3711
Caseys #3835

or Allegiant Airlines:

Allegnt*air Bnfdmx
Allegnt*air Bnz4d7
Allegnt*air Bphrlk


To reiterate, my original thought was to do a find and replace for say, "Caseys" and for every instance of that found variable, the entire cell's contents would then be replaced with "Casey's General Store". I fully understand VBA cannot guess what I want it to do.

So maybe the best option would be to add the first iteration of your code and then run this second step for the remaining ones that need correcting. However, it still leaves me with the question, is there some code that can either:

1) replace the original cell contents with user inputted replacement text if certain text is found within that cell
2) enter the replacement text in an adjacent cell

Sorry if I'm not providing enough data or making this more difficult than it should be and again, thank you very much for your help and efforts!
 
Upvote 0

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 don't think the new code would work because the vendor names can vary so much. For example, this current month has all of the following names which should be changed to Casey's General Store:

Caseys #2080
Caseys #2587
Caseys #2605
Caseys #3272
Caseys #3272
Caseys #3272
Caseys #3272
Caseys #3711
Caseys #3835

or Allegiant Airlines:

Allegnt*air Bnfdmx
Allegnt*air Bnz4d7
Allegnt*air Bphrlk
I think you misunderstood how my code works. You do NOT need to list each one separately. Just list the common prefix.

So for all your "Caseys..." entries, you only need one line:

Value to find in column N: "Caseys"
Value to replace in column O: "Casey's General Store"

The reason it works is because I am adding a wildcard to the end of the Find value (perhaps you missed that):
Rich (BB code):
Columns("C:C").Replace What:=fnd & "*",...

So basically, what it is saying (in this example) is Find and Replace any entry that STARTS WITH "Caseys" (it does NOT need to equal it exactly).
 
Upvote 0
OK, thank you. It's going to take a bit of time to get the data in order because 3 other columns rely on that same data for their internalid's so I think I'll need to mess with it some. Thank you for your patience with me!
 
Upvote 0
You are welcome.
Feel free to post back to this thread if you run into any issues with this particular question.

I hope you see why I was asking all those questions. The code is so dependent upon all those little details.
Hopefully, it kind of gives you an idea of the level of information that needs to be provided in order to get solutions that will really work on your data, and you will have a better idea of what to think about and include in future questions.

Happy Excel-ling!
:)
 
Upvote 0
You are welcome.
I am actually going to re-mark the solution as the post that contain the last updated code, as the post you marked isn't anything new, just explaining what the code does.
We typically want to mark the post that contains the solution.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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