Solved

VBA Check for Character

Posted on 2016-07-21
9
76 Views
Last Modified: 2016-07-21
I would like to check with a function whether a string contains a character from a-z.
As you can see, I use the LIKE operator. The problem is that sometimes the function works, sometimes it doesn't

When I check for the following value, the function returns FALSE, instead of TRUE: 15.fdas45.fsda

Any ideas what I'm doing wrong?

Function CheckForLetter(strSearch As String) As Boolean

    Dim intPosition As Long
    CheckForLetter = True

    For intPos = 1 To Len(strSearch)
        CheckForLetter = (Mid$(strSearch, intPosition, 1) Like "[a-zA-Z]")
        If Not CheckForLetter Then
        Exit For
        Exit Function
    Next intPosition

End Function

Open in new window

0
Comment
Question by:mscola
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41723038
your function returns false on the first character that is not a-z...
reverse tge logic, hence
0
 
LVL 32

Assisted Solution

by:Rob Henson
Rob Henson earned 50 total points
ID: 41723042
How about checking the ASCII Chr value of the character?

A to Z = 65 to 90
a to z = 97 to 122
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 450 total points
ID: 41723053
It should be like this.....

Function CheckForLetter(strSearch As String) As Boolean
Dim intPosition As Long
For intPosition = 1 To Len(strSearch)
   CheckForLetter = (Mid$(strSearch, intPosition, 1) Like "[a-zA-Z]")
   If CheckForLetter Then Exit For
Next intPosition
End Function

Open in new window

0
 

Author Closing Comment

by:mscola
ID: 41723070
Neerja's solution worked best for me.

Thanks for your help!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41723074
You're welcome. Glad we could help.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41723086
Sure. It does exactly what i wrote...
Relaxing on tbe beach i didn't want to write out the actual code ( trying to make people yealous ;-)
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41723108
Also I think it's best to check if a letter exists in a string at once instead of looping through each character in string.

Alternatively you may also use the following function. See if that helps...

Function CheckForLetter(strSearch As String) As Boolean
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
With RE
   .IgnoreCase = True
   .Pattern = "[A-Z]"
End With
CheckForLetter = RE.Test(strSearch)
End Function

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41723131
would this not work much faster

if yourstring like "*[a-zA-Z]*" then checkexistance = true
1
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41723155
Wonderful! This shows why you are the Top Expert In EE. :)

CheckForLetter = strSearch Like "*[a-zA-Z]*"

Also no need for function as one single line can be used in the code itself like

IF strSearch Like "*[a-zA-Z]*" Then
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now