Solved

VBA Excel 2010 verify userform textbox input as text

Posted on 2013-01-16
9
2,428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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 48

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:tom_burkhardt
ID: 38783654
@imnorie: yes, I want letters only. No numbers, no special characters.
0
 
LVL 48

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 48

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

696 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