Solved

VBA Excel 2010 verify userform textbox input as text

Posted on 2013-01-16
9
2,225 Views
Last Modified: 2013-01-16
Learning VBA for Excel. I'm using Excel 2010.

I have a userform with an input text field named "TextName".  
When the user clicks a button, the content of the textbox is added to a new row at the bottom of a worksheet column.

my question:
How do I force the content to be text only?

Here's what I'm trying, but it does not work:
    If Not Application.Workbooks.IsText(TextName.Text) Then
        MsgBox "Please enter text only."
        TextName.SetFocus
        Exit Sub
    End If
0
Comment
Question by:tom_burkhardt
9 Comments
 
LVL 13

Expert Comment

by:Shanan212
ID: 38783634
try this (checks for numbers, if not text is assumed)

If isnumeric(TextName.Text) Then

Open in new window

0
 
LVL 33

Accepted Solution

by:
Norie earned 350 total points
ID: 38783641
Whatever is entered in a textbox is text.

Do you want to check that only a-z/A-Z are entered?
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 38783647
    Dim lngIndex As Long
    For lngIndex = 1 To Len(TextName.Text)
        If IsNumeric(Mid(TextName.Text, lngIndex, 1)) Then
            MsgBox "Please enter text only."
            TextName.SetFocus
            Exit Sub
        End If
    Next

Open in new window

0
 

Author Comment

by:tom_burkhardt
ID: 38783654
@imnorie: yes, I want letters only. No numbers, no special characters.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Expert Comment

by:Martin Liss
ID: 38783661
Please see my code.
0
 

Author Comment

by:tom_burkhardt
ID: 38783669
@MartinLiss: thanks, but your code would allow special characters (*&^%$#@  etc.) through
0
 

Author Comment

by:tom_burkhardt
ID: 38783706
thanks all, I got it. credit to imnorie for putting me on the right track

    If Not (TextName.Text Like "[A-Z,a-z]") Then
        MsgBox "Please enter text only."
        TextName.SetFocus
        Exit Sub
    End If
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 38783716
Change line 3 to


        If Asc(UCase(Mid(TextName.Text, lngIndex, 1))) < 65 Or Asc(UCase(Mid(TextName.Text, lngIndex, 1))) > 90 Then
0
 

Author Comment

by:tom_burkhardt
ID: 38783781
my apologies to MartinLiss, I'd already given the points to inmorie.
After running the code in my accept note, yes it blocked numbers but it also blocked letters, which I should have checked more thoroughly.
However for anyone else with this question , here is a function that will validate input from a textbox to ensure letters only ...

Function IsLetter(strValue As String) As Boolean
  Dim myInt As Integer  
  IsLetter = True
 
  For myInt = 1 To Len(strValue)
    Select Case Asc(Mid(strValue, myInt, 1))
    Case 65 To 90, 97 To 122
      ' do nothing, leave as TRUE
    Case Else
      ' not a letter, return FALSE
      IsLetter = False
      Exit For
    End Select
  Next myInt
End Function


so in the calling Sub, you would have:
    If Not IsLetter(TextName.Text) Then
        MsgBox "Please enter text only."
        TextName.SetFocus
        Exit Sub
    End If
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

708 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

15 Experts available now in Live!

Get 1:1 Help Now