User Defined Function needed to replace nested substitute formula

I need a UDF to use instead of a bunch of nested Substitute functions.  I have a form in which a user enters paragraphs of text.  That text is uploaded to a program that cannot process certain characters. I currently use nested substitute functions in a field hidden from the user.  As this form grows in size, the nested substitute formula is feeling clunky and inefficient. I have used it in many dozens of fields and the file size is growing. Basically, there are 10 specific characters which I want to replace.

The current formula is =TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(AA1046,CHAR(34),"'"), CHAR(145),"'"), CHAR(146),"'"), CHAR(147),"'"), CHAR(148),"'"), CHAR(150),"-"), CHAR(151),"-"), CHAR(9),"  "), CHAR(10)," "),CHAR(12),CHAR(7)))))
Kathy ChristensenAsked:
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.


pls try
Function myFunction(strText As String) As String
myFunction = Evaluate("=TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(" & strText & ",CHAR(34),""'""), CHAR(145),""'""), CHAR(146),""'""), CHAR(147),""'""), CHAR(148),""'""), CHAR(150),""-""), CHAR(151),""-""), CHAR(9),""  ""), CHAR(10),"" ""),CHAR(12),CHAR(7)))))")
End Function

Open in new window

NorieAnalyst Assistant Commented:

Try this.
Function CleanMyString(strTxt As String) As String
Dim arrBadChars As Variant
Dim arrReplChars As Variant
Dim I As Long

    arrBadChars = Array(Chr(34),Chr(145),Chr(146),Chr(147),Chr(148),Chr(150),Chr(151),Chr(9),Chr(10),Chr(12))
    arrReplChars =Array("'","'","'","'","'","-","-","  "," ",Chr(7))

    CleanMyString = strTxt

    For I = LBound(arrBadChars) To UBound(arrBadChars)
        CleanMyString = Replace(CleanMyString, arrBadChars(I), arrReplChars(I))
   Next I

End Function

Open in new window

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
Kathy ChristensenAuthor Commented:
Thank you both for your answers.  The one posted by @Rgonzo1971 gave me a #Value error.  The one posted by @Norie worked great!
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 Excel

From novice to tech pro — start learning today.