VBA Replace

gerald24

Board Regular
Joined
Apr 28, 2017
Messages
95
Hi Guys,

Suppose I have a list on text that I want to replace a specific part of it with something.


Column A
11346 XZ Alpha
91970 XZ Alpha
113974 YP Alpha
1135 YP Alpha
99786 XY Alpha
99786 XY


from XZ to ZZ
from YP to MS
from XY to JA

In my current code, I recorded a find/replace code.

What I desire to accomplish is for it to replace those codes in the middle or even if the word "Alpha" is not present, I still want to replace the "XY". I have hundreds of this code, but I just need to have a sample how can I code this?

Thanks!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The reason why I need to move from "Find / Repalce" to a better code is that some text in the first part of the cell value contains "XZ". For Example, 11XZ39 YP Alpha, I just need it to be 11XZ39 MS ALPHA and not 11ZZ39 Alpha.
 
Upvote 0
Instead of find "XZ" replace "ZZ" use :

find " XZ" replace " ZZ"
 
Upvote 0
Don't replace "YP", rather, replace " YP " (note the surrounding spaces) with " MS " (again, not the surrounding spaces). Do the same for your other replacements.
 
Upvote 0
Hi Guys,

I actually did that too already, but forgot to mention, I also have "AL to NP", which make my Alpha to "Nppha". Tried to set Case Sensitive to True, but my data contains "Alpha" and "ALPHA".
 
Upvote 0
Hi Guys,

I actually did that too already, but forgot to mention, I also have "AL to NP", which make my Alpha to "Nppha". Tried to set Case Sensitive to True, but my data contains "Alpha" and "ALPHA".

I suppose you could perform a final Find Replace of "Nppha" with "Alpha".
 
Upvote 0
But your suggestion doesn't work on the last example in post #1 : 99786 XY

Another thing is that, XZ might appear on the first part of the text. like XZ1346 YP ALPHA, in which " XZ" might replace. which means the "space" technique might get the best of my data.


I am supposing that I can use the below formula

=IF(RIGHT(A1,6)=" ALPHA",REPLACE(LEFT(A1,LEN(A1)-6),LEN(A1)-7,2,VLOOKUP(RIGHT(LEFT(A1,LEN(A1)-6),2),$A$3:$B$5,2,0)&" ALPHA"),REPLACE(A1,LEN(A1)-1,2,VLOOKUP(RIGHT(A1,2),$A$3:$B$5,2,0)))

let's say that my text is in A1 and I have a mapping in A3:C5.

I just don't know how to put this in codes. I don't really prefer maintaining a mapping.
 
Upvote 0
Code:
[COLOR=#333333]Another thing is that, XZ might appear on the first part of the text. like XZ1346 YP ALPHA, in which " XZ" might replace. which means the "space" technique might get the best of my data.[/COLOR]

If you need help, you will have to stop moving the goalposts.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
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