• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 123
  • Last Modified:

VBA Check for Character

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
Massimo Scola
Asked:
Massimo Scola
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
your function returns false on the first character that is not a-z...
reverse tge logic, hence
0
 
Rob HensonFinance AnalystCommented:
How about checking the ASCII Chr value of the character?

A to Z = 65 to 90
a to z = 97 to 122
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Massimo ScolaAuthor Commented:
Neerja's solution worked best for me.

Thanks for your help!
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome. Glad we could help.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
would this not work much faster

if yourstring like "*[a-zA-Z]*" then checkexistance = true
1
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now