Vba restrict activex textbox to numeric values in Excel

For example, I have a ActiveX Text Box (TextBox1) - I want to prevent users from entering anything other than numbers in this TextBox.

To do it in Excel, here is the answer:

  1. Option Explicit
  2. Private Sub TextBox1_Change()
  3. If Not IsNumeric(TextBox1.Value) Then
  4. MsgBox "Only numbers are allowed"
  5. Cancel = True
  6. End If
  7. End Sub


a) Line 1 - Whenever there is a change in TextBox Value, the code in routine is executed.

b) Line 2 - Check if Input is a numeric value.

c) Line 3 - If Input is not a numeric value, prompt user.

Note: The code above has to reside in the Sheet corresponding to where the ActiveX control resides. For example, if TextBox1 ActiveX Control is in Sheet1, the code should reside in Sheet1 in VBA Module.


You can find similar Excel Questions and Answer hereunder

1) Here some explanations about the MSXML who stands for Microsoft XML core services

2) How can I sort data using VBA?

3) How can I find the number of working days between 2 dates using VBA?

4) How can I hide Formula Bar and Headings using VBA?

5) How can I set the Source Data of charts using VBA?

6) Generate a list of unique values in Excel

7) How can I add a Timestamp after macro execution?

8) How can I loop through all WorkSheets and get their names using VBA?

9) How to handle errors in VBA with the handling error methods

10) How to copy files in Excel VBA is explained here


Here the previous and next chapter