Checking for special characters in a string

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
 
C

Chip Pearson

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top