Find and replace text

bobkap

Active Member
Joined
Nov 22, 2009
Messages
323
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
As part of an Excel macro, I'm trying to find if a string has a specific word in it and then, if it does, copy the string to another cell without some of the words in the cell.

I've tried this (below) which works ok, BUT if the address you see below is not EXACTLY the same which is often the case with a moved space or a deleted comma etc, the trim function will not work of course. I know there must be a way to do some kind of wildcard solution, but darned if I can figure it out.

Here's what I've tried so far:
grape = Cells(mirow, micol)
If InStr(grape, "San Ramon") Then
bk = Trim(Replace(grape, "(211 Old Crow Canyon Rd#19, San Ramon Ca 94583)", " "))
Cells(mirow, micol +1) = bk

Any help would be very greatly appreciated.
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I think we need more more details and examples.
Can you show us a few examples of what you are trying to do, including some records where it is not working correctly?
 
Upvote 0
I think we need more more details and examples.
Can you show us a few examples of what you are trying to do, including some records where it is not working correctly?

Thanks for asking.
Here's an example of two of the records:
[TABLE="width: 722"]
<tbody>[TR]
[TD="width: 722"]Course 8:00am to 3:00pm Nov 16th 2017(211 Old Crow Canyon Rd,#19 San Ramon Ca 94583)
[TABLE="width: 722"]
<tbody>[TR]
[TD="width: 722"]Course 8:00am to 3:00pm Nov 16th 2017(211 Old Crow Canyon Rd, #19 San Ramon Ca 94583)
Here's output I need:
[TABLE="width: 722"]
<tbody>[TR]
[TD="width: 722"]One cell: Course 8:00am to 3:00pm Nov 16th 2017
Next cell one column over: San Ramon

I've figured out how to identify the records I need changed and how to trim down the text and copy over to the correct cells. BUT, only by inputting the exact words I want replaced with nothing (""). As you can see in my two examples, there is a space before the "#" in one but not in the other. So, only one will be chosen. I was hoping that I could use some kind of wildcard code so that I could just use one unique word in my string and then I wouldn't have to tweak my code for every eventuality of an extra space, comma etc.[/TD]
[/TR]
</tbody>[/TABLE]

[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Work on having your code standardize the data first.
In this case, if you did a Find/Replace finding a comma and hashtag and replacing with a comma, space, and hashtag, I think that should do it.
Then all your data would be the same.
 
Upvote 0
Awesome thinking. Thanks! I NEVER would have considered that in maybe 100 years.

Regards,
Bob
 
Upvote 0
You are welcome!

Here is another "trick" that may come in handy at home point. Let's say that you had a similar situation except that the first non-space character after the comma is not always a hashtag, and you needed a single space between the comma and the next non-space character. This can be accomplished with a two-part Find/Replace.
First, replace a single comma with a comma and a space.
Then, replace two spaces with a single space.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
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