ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Checking for special characters in a string (https://www.excelbanter.com/excel-programming/423421-re-checking-special-characters-string.html)

Rick Rothstein

Checking for special characters in a string
 
Here is a more compact function that works exactly like your function
does...

Function HasBadChar(S As String) As Boolean
HasBadChar = S Like "*[!0-9A-Za-z_]*"
End Function

--
Rick (MVP - Excel)


"Chip Pearson" wrote in message
...
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)




On Wed, 4 Feb 2009 12:14:02 -0800, John
wrote:

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




All times are GMT +1. The time now is 10:03 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com