Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA Check for Character

Posted on 2016-07-21
9
Medium Priority
?
106 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:Massimo Scola
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 143

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 33

Assisted Solution

by:Rob Henson
Rob Henson earned 200 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 32

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 1800 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

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

Thanks for your help!
0
 
LVL 32

Expert Comment

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

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 32

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 143

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 32

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

730 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