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!”
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

Leave a Reply

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