Solved

VBA Excel 2010 verify userform textbox input as text

Posted on 2013-01-16
9
2,260 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 46

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 46

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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

27 Experts available now in Live!

Get 1:1 Help Now