Use VBA to check if a cell in Excel contains letters

What is the simplest way to tell if a cell contains any letters? I'm not looking for specific letters and it doesn't matter if the cell contains any numbers or special characters. If the cell contains one or more of the letters from A-Z, I would like the test to be positive regardless of whatever else is in the cell.

Thanks in advance.
jrmcanada2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ProfessorJimJamCommented:
you can use the worksheet funcction in VBA

simple as this

Sub test()

For Each C In Selection
If Application.WorksheetFunction.IsText(C.Value) Then
MsgBox "is text " & C.Address
End If
Next C
For Each C In Selection
If Application.WorksheetFunction.IsNumber(C.Value) Then

MsgBox "is number " & C.Address

End If
Next C
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zorvek (Kevin Jones)ConsultantCommented:
Use this formula:

=IF(SUMPRODUCT((CODE(LOWER(MID(A1,COLUMN(INDIRECT("A:"&CHAR(64+LEN(A1)))),1)))=ROW($96:$123))*1)>0,"Contains characters","Does not contain characters")

Kevin
0
ProfessorJimJamCommented:
if you have different text and numbers in different cells, then select them and then run the macro, then it will tell which cell address whether it is text or whether it is numeric
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

zorvek (Kevin Jones)ConsultantCommented:
This version handles empty cells:

=IFERROR(IF(SUMPRODUCT((CODE(LOWER(MID(A1,COLUMN(INDIRECT("A:"&CHAR(64+LEN(A1)))),1)))=ROW($96:$123))*1)>0,"Contains characters","Does not contain characters"),"Cell is empty")

In the formula, the reference A1 is the cell being evaluated. Change as required.

Kevin
0
zorvek (Kevin Jones)ConsultantCommented:
I neglected to see the desire for a VBA solution.

Kevin
0
ProfessorJimJamCommented:
yes, Zorvek.

i totally agree with you. when it can be done using built in functions, why to overkill it with VBA.

your solution is good
0
zorvek (Kevin Jones)ConsultantCommented:
Here is a VBA function you can use to test specifically for upper and lower case letters:

Public Function HasCharacters(ByVal Text As String) As Boolean

    Dim Position As Long
   
    HasCharacters = True
   
    For Position = 1 To Len(Text)
        If UCase(Mid(Text, Position, 1)) >= "A" And UCase(Mid(Text, Position, 1)) <= "Z" Then Exit Function
    Next Position
   
    HasCharacters = False

End Function

Use it like so:

If HasCharacters(CellReference.Value) Then

Kevin
0
ThomasMcA2Commented:
VBA claims to support Regular Expressions, so this should work:

If .Range("A1").Value like "[a-zA-Z]" Then
   Msgbox "Contains letters!"
End if

Open in new window


However, when I tested that with Excel 2013, it did not work. MS removed a lot of functionality in all of their 2013 products, so YMMV.
0
zorvek (Kevin Jones)ConsultantCommented:
A couple of points. The "Like" operator is not Regular Expressions. It is powerful though but, to provide the desired result, we have to include the asterisk on both ends to account for other characters. And it does work in all versions of Excel.

Public Function HasCharacters(ByVal Text As String) As Boolean

    If Text Like "*[a-zA-Z]*" Then
       HasCharacters = True
    End If

End Function

Kevin
0
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
sai kCommented:
"This version handles empty cells:

=IFERROR(IF(SUMPRODUCT((CODE(LOWER(MID(A1,COLUMN(INDIRECT("A:"&CHAR(64+LEN(A1)))),1)))=ROW($96:$123))*1)>0,"Contains characters","Does not contain characters"),"Cell is empty")

In the formula, the reference A1 is the cell being evaluated. Change as required."


zorvek, can you please elaborate this?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.