ISASCII function

C

Charlie Mac

VBA Gerus,

Is there a function in VBA to derermine if a character is an ASCII
character (A - Z or a - z)? I need something like this: msgbox
IsASCII(aWord) returning a one or zero.

Thanks.

Charlie from Texas
 
J

Jay Freedman

VBA Gerus,

Is there a function in VBA to derermine if a character is an ASCII
character (A - Z or a - z)? I need something like this: msgbox
IsASCII(aWord) returning a one or zero.

Thanks.

Charlie from Texas

To correct a misconception, ASCII (now called ANSI) characters include much more
than A-Z and a-z -- the digits, punctuation, and all other characters in the
range from 0 to 255 are 'ASCII characters'. So I wrote the function name as
IsAlpha instead.

Function IsAlpha(s As String) As Boolean
Dim retVal As Boolean
retVal = False

If Len(s) > 0 Then
If Left$(s, 1) Like "[A-Za-z]" Then
retVal = True
End If
End If

IsAlpha = retVal
End Function

Sub test()
Dim myText As String
Dim myChar As String
Dim msg As String
Dim i As Long

myText = Selection.Text
For i = 1 To Len(myText)
myChar = Mid$(myText, i, 1)
msg = "'" & myChar & "' is "
If Not IsAlpha(myChar) Then
msg = msg & "NOT "
End If
msg = msg & "in [A-Za-z]"
MsgBox msg
Next
End Sub

As written, this function checks only one character, the first one in the string
passed in as the parameter. If you meant to check a whole word or an entire
string, that would need some slightly different logic.
 
C

Charlie Mac

Jay,

Thank you for your reply. I did not know that the "Like" function
existed. Your code solved my problem.

Take care,

Charlie from Texas

VBA Gerus,

Is there a function in VBA to derermine if a character is an ASCII
character (A - Z or a - z)? I need something like this: msgbox
IsASCII(aWord) returning a one or zero.

Thanks.

Charlie from Texas

To correct a misconception, ASCII (now called ANSI) characters include much more
than A-Z and a-z -- the digits, punctuation, and all other characters in the
range from 0 to 255 are 'ASCII characters'. So I wrote the function name as
IsAlpha instead.

Function IsAlpha(s As String) As Boolean
Dim retVal As Boolean
retVal = False

If Len(s) > 0 Then
If Left$(s, 1) Like "[A-Za-z]" Then
retVal = True
End If
End If

IsAlpha = retVal
End Function

Sub test()
Dim myText As String
Dim myChar As String
Dim msg As String
Dim i As Long

myText = Selection.Text
For i = 1 To Len(myText)
myChar = Mid$(myText, i, 1)
msg = "'" & myChar & "' is "
If Not IsAlpha(myChar) Then
msg = msg & "NOT "
End If
msg = msg & "in [A-Za-z]"
MsgBox msg
Next
End Sub

As written, this function checks only one character, the first one in the string
passed in as the parameter. If you meant to check a whole word or an entire
string, that would need some slightly different logic.
 

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