VBA easy way to replace characters in a cell

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a range of strings that contain different values in which I need to replace.
Is there an easy way to replace these characters:

"'", "2", "5", "@", "Q", "v", "b", "g", "v", "{" to 0
"%", "/", ">", "?", "N", "W", "]", "i", "u", "|" to 1

If I have
2?@55| then I need the output to be 00551
>116v742W then I need the output to be 11607401


Thanks in advance
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
This is confusing. If 5 needs to changed to 0 then why is 2?@55| output meant to be 00551? Anyway you not said where these are or where they need to go so heres a start:

VBA Code:
Function ChangeChars(str As String)

Dim chars As String, replaceChars As String
Dim a As String, b As String, i As Integer

chars = "'25@Qvbgv{%/>?NW]iu|"
replaceChars = "00000000001111111111"

For i = 1 To Len(chars)
    a = Mid(chars, i, 1)
    b = Mid(replaceChars, i, 1)
    str = Replace(str, a, b)
Next

ChangeChars = str

End Function
 
Upvote 0
Steve the Fish, Sorry you are spot on. I did make a mistake with the 5 you output is correct and I thank you for getting me moving on this. I appreciate you help!!!
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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