Reputation: 3543
I need a function which completely deletes all special characters (visible/invisible) from a string besides A-Z, a-z, 0-9. I have already prepared a function, unfortunately I find out it's leaving commas ( , ) somehow, and maybe more I don't know. Could you please take a look and advise:
Function RemoveSpecialChars(Tekst As String) As String
Dim a$, b$, c$, i As Integer
a$ = Tekst
For i = 1 To Len(a$)
b$ = Mid(a$, i, 1)
If b$ Like "[A-Z,a-z,0-9]" Then
c$ = c$ & b$
End If
Next i
RemoveSpecialChars = c$
End Function
Upvotes: 4
Views: 3206
Reputation: 26259
Just remove the commas:
If b$ Like "[A-Za-z0-9]" Then
As it says on MSDN for Visual Basic:
To specify multiple ranges for the same character position, put them within the same brackets without delimiters. For example, [A–CX–Z] results in a match if the corresponding character position in string contains any character within either the range A–C or the range X–Z.
Although MSDN doesn't specifically mention this for VBA, it shows the same behaviour.
Upvotes: 4