Solved

VBA Excel 2010 verify userform textbox input as text

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

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

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

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 47

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

739 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