Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 113
  • 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 HensonIT & Database AssistantCommented:
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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