Create custom Replace function

Hi Experts,

I am looking to create a function that will accept a string and only return the alpha/numeric values, except for dashes all special characters should get stripped out.

So instead of having to use the following
fieldName = Left(Replace(Replace(Replace(Mid(ColumnName, 2, Len(ColumnName) - 2), " ", ""), "&", ""), "+", ""), 32)

Open in new window

I can call my function something like
MyReplace(sTrVar,"_")

Open in new window


P.S. If possible, would prefer to have the 2nd variable of the function (the one holding the dash) capable of managing more than one character as an exception, so I can send "_,-,' ') and all 3 of them would be considered valid characters, not getting stripped out.

Thanks
LVL 6
bfuchsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel PineaultPresident / Owner CARDA Consultants Inc.Commented:
RegEx might be a good option for this.

See: https://stackoverflow.com/a/43166192/9706001
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Here's a good article on EE about regex:

https://www.experts-exchange.com/articles/1336/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html

 beyond that, there is a lot of generic "find and replace" code floating around.    But this seems specific enough for you to write your own.

 It's simple enough....just loop through the string and dump anything you don't want.   Generally done with an In and out string, the latter of which you build up as you read the "in" string.

Jim.
Zakaria AcharkiAnalyst DeveloperCommented:
You could use regex, this one will help you :

[^A-Za-z\d_]

Open in new window


You could add as many special characters you want to ignore, that you will pass in the second argument like :

"[^A-Za-z\d" & ingoredChars & "]"

Open in new window


You just need to pass the ingoredChars together like "_-,'" in the second argument.

The whole function code could be something like :

Private Function MyReplace(ByVal strVar As String, ByVal ingoredChars As String) As String
    Set objRegExp_1 = CreateObject("vbscript.regexp")
    objRegExp_1.Global = True
    objRegExp_1.IgnoreCase = True
    objRegExp_1.Pattern = "[^A-Za-z\d" & ingoredChars & "]"
    
    MsgBox ( objRegExp_1.Replace(strVar, "") )
    
End Function

Open in new window


And you could call it like :

MyReplace("my_-string;*with#special.:;characters/","_;*")

Open in new window


Not tested yet.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

bfuchsAuthor Commented:
@Zakaria,

Middle of testing yours...seems to work-:)

Can you explain why do I get into the debug line?
fieldName = Left(Replace(Replace(Replace(Replace(Mid(ColumnName, 2, Len(ColumnName) - 2), " ", ""), "&", ""), "+", ""), "-", ""), 32)
                        If fieldName <> Left(MyReplace(Mid(ColumnName, 2, Len(ColumnName) - 2), "_"), 32) Then
                            Debug.Print fieldName
                        End If

Open in new window


See attached.

Thanks,
Ben
Untitled.png
bfuchsAuthor Commented:
Thanks to all participants!
Zakaria AcharkiAnalyst DeveloperCommented:
You're welcome @bfuchs, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.