Got any Excel/VBA Questions? Excel Help
Validating UserForm TextBox to Only Accept Text
If you are fairly comfortable with Excel VBA you will most likely want to design a UserForm to ensure correct use of your Spreadsheet. However, just placing some TextBoxes on a UserForm for users to enter data is sometimes not enough. For example, the TextBoxes may be intended to take only text data and not numbers. To do this you can use some code as shown below.
Dim bNumbers As Boolean Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) OnlyText Cancel = bNumbers End Sub Private Sub OnlyText() If TypeName(Me.ActiveControl) = "TextBox" Then If ActiveControl = vbNullString Then Exit Sub If IsNumeric(ActiveControl) Then MsgBox "Sorry, text only" ActiveControl = vbNullString bNumbers = True End If End If End Sub
As with any UserForm Control code, this code must reside in the Private Module of the UserForm Object
|Index to Excel VBA Code|
|Add Excel UDF/Custom Function to a Category & Add a Description|
|The Ultimate Excel Lookup Function|
|Update Links in Excel|
|Stop UserForm From Closing via X|
|Excel VBA Code For Excel UserForms & Controls|
|TextBox for Numbers Only|
See also Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions.
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.