I need a function in VBA like "isNumeric" that checks if a string only
contains alaphabetic characters. Would love some help.
Thanks
CR
I need a function in VBA like "isNumeric" that checks if a string only
contains alaphabetic characters. Would love some help.
Thanks
CR
how about
Function IsAlpha(s as string)
IsAlpha = (ucase(s) like [A-Z])
end function
?
Tim
"C. Roenbaugh" <[email protected]> wrote in message
news:[email protected]...
>I need a function in VBA like "isNumeric" that checks if a string only
> contains alaphabetic characters. Would love some help.
>
> Thanks
>
> CR
>
Thanks Tim
Here is what i used, but i will try your suggestion.
nonAlpha = 0
For j = 1 To Len(userPassword)
Select Case Asc(Mid(userPassword, j, 1))
Case 0 To 47, 58 To 64, 123 To 256
nonAlpha = nonAlpha + 1
Case 48 To 57, 65 To 122
nonAlpha = nonAlpha + 0
End Select
Next j
Function IsAlpha(s As String)
IsAlpha = (UCase(s) Like "[A-Z]")
End Function
Think it would be a little more complex than that:
? isalpha("abc")
False
--
Regards,
Tom Ogilvy
"Tim Williams" <saxifrax at pacbell dot net> wrote in message
news:[email protected]...
> how about
>
> Function IsAlpha(s as string)
> IsAlpha = (ucase(s) like [A-Z])
> end function
>
> ?
> Tim
>
> "C. Roenbaugh" <[email protected]> wrote in message
> news:[email protected]...
> >I need a function in VBA like "isNumeric" that checks if a string only
> > contains alaphabetic characters. Would love some help.
> >
> > Thanks
> >
> > CR
> >
>
>
I believe this does what you're asking for:
Function IsAlpha(entry) As String
Dim i As Integer
Dim c As Variant
Dim OK As Boolean
i = 1
For i = 1 To Len(entry)
c = Mid(entry, i, 1)
If (UCase(c) Like "[A-Z]") Then
OK = True
Else
OK = False
End If
If OK = False Then
IsAlpha = False
Exit Function
End If
Next i
IsAlpha = True
End Function
Regards,
Anita
C.R.,
If you want to check for the condition that ALL characters in a STRING are alphanumeric and that the presence of even one non-alphanumeric should return "FALSEHOOD", you may need to tweak your code thus:
Function CheckForAlphaNumeric(txt As String)
For j = 1 To Len(txt)
Select Case Asc(Mid(txt, j, 1))
Case 0 To 47, 58 To 64, 91 To 96, 123 To 256
CheckForAlphaNumeric = "NON-ALPHANUMERIC FOUND"
Exit Function
End Select
Next j
CheckForAlphaNumeric = "ALL ALPHANUMERIC"
End Function
Note that I have had to recalibrate the select case ranges to fully segregate the alphanumerics from the non-alphanumerics.
If you wish to check for ALPHABETS ONLY (and not ALPHANUMERICS as your attempted code suggested), then use:
Function CheckForAlphasOnly(txt As String)
For j = 1 To Len(txt)
Select Case Asc(Mid(txt, j, 1))
Case 0 To 64, 91 To 96, 123 To 256
CheckForAlphasOnly = "NON-ALPHAS FOUND"
Exit Function
End Select
Next j
CheckForAlphasOnly = "ALL ALPHAS"
End Function
Last edited by Myles; 01-23-2006 at 12:22 AM.
Without using a Regular Expression, would this work for you?
Function AllAlpha(s As String) As Boolean
AllAlpha = UCase(s) Like WorksheetFunction.Rept("[A-Z]", Len(s))
End Function
HTH :>)
--
Dana DeLouis
Win XP & Office 2003
"C. Roenbaugh" <[email protected]> wrote in message
news:[email protected]...
>I need a function in VBA like "isNumeric" that checks if a string only
> contains alaphabetic characters. Would love some help.
>
> Thanks
>
> CR
>
My bad - poor reading of the Help...
Tim
"Tom Ogilvy" <[email protected]> wrote in message
news:%[email protected]...
> Function IsAlpha(s As String)
> IsAlpha = (UCase(s) Like "[A-Z]")
> End Function
>
> Think it would be a little more complex than that:
>
> ? isalpha("abc")
> False
>
> --
> Regards,
> Tom Ogilvy
>
>
> "Tim Williams" <saxifrax at pacbell dot net> wrote in message
> news:[email protected]...
>> how about
>>
>> Function IsAlpha(s as string)
>> IsAlpha = (ucase(s) like [A-Z])
>> end function
>>
>> ?
>> Tim
>>
>> "C. Roenbaugh" <[email protected]> wrote in message
>> news:[email protected]...
>> >I need a function in VBA like "isNumeric" that checks if a string only
>> > contains alaphabetic characters. Would love some help.
>> >
>> > Thanks
>> >
>> > CR
>> >
>>
>>
>
>
of all the replies I've seen, I think that your own code in the end is the
better INHO
The cjhnage I made was to specify what IS a good character - its a heck
easier that excluding stuff....
Function IsAlpha(s As String) As Boolean
Dim letter As String
Dim index As Long
' note defailt is FALSE
IsAlpha = True
For index = 1 To Len(s)
letter = Mid(s, index, 1)
Select Case Asc(letter)
Case 32, 48 To 57, 65 To 90, 97 To 122: 'OK
Case Else
IsAlpha = False
Exit For
End Select
Next
End Function
"C. Roenbaugh" wrote:
> I need a function in VBA like "isNumeric" that checks if a string only
> contains alaphabetic characters. Would love some help.
>
> Thanks
>
> CR
>
>
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks