Limit user input in a text box or an Excel Worksheet Cell

Microsoft Excel VBA Coding – Limit user input on a text box How can I limit the user’s input in a text box on a user form or a Microsoft worksheet cell? Thanks for your help in advance! Margaret Often when we enter data in an Excel worksheet we need to input a specific size or length of data as in the case of a zip code or a telephone number. In order to help the user we can limit this data entry using data validation or a macro. Mistakes in data entry can thus be avoided. Also we can have a user-form with text boxes where the user enters a telephone number. Here also we can help the user by allowing only a certain number of digits to be entered.
Useful reference macro code:
Private Sub CommandButton1_Click()
If Len(TextBox2.Text) <> 10 Then
MsgBox “The mobile number can only be 10 digits. Pls correct!”
Else
eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(eRow, 1) = TextBox1.Text
Cells(eRow, 2) = TextBox2.Text
End If
End Sub
Watch the training video below to learn how this can be implemented in an Excel worksheet as well as a userform:


Further reading: Data Validation Designate valid cell entries

One thought on “Limit user input in a text box or an Excel Worksheet Cell

  1. Respected Sir,
    I want to enter the marks of a student in VBA userForm. There’ll be two different types of marks to be entered. One for practical ie of maximum 30 and another for practical ie of maximum 70 marks respectively. The total marks should auto generate. I am trying my best from the link and the resources you shared to me, but I am helpless. Sir, this is my humble request if you can help me to solve this problem I would be much more grateful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.