Checking for special characters in a string

  • Thread starter Thread starter John
  • Start date Start date
J

John

How do I check a string to see if it contains any special characters like @$%
etc?

I'm using InputBox to request a name that will be used to define a range
name and range names can consist of alpha, numeric and underscore characters
only.

I appreciate your help, -John
 
You can use a function like

Function HasBadChar(S As String) As Boolean
Dim N As Long
For N = 1 To Len(S)
Select Case Mid(S, N, 1)
Case "A" To "Z", "a" To "z", "0" To "9", "_"
' OK
Case Else
HasBadChar = True
Exit Function
End Select
Next N
HasBadChar = False
End Function

It will return True if there is any character in string S other than
letters, numbers, and underscores. If the string has no other
characters, it returns False.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
Back
Top