Solved

VBA Check for Character

Posted on 2016-07-21
9
71 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 31

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Outlook Free & Paid Tools
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

17 Experts available now in Live!

Get 1:1 Help Now