How can I ensure that users enter only numeric values and display an error message if they input non-numeric characters? Additionally, is there a way

alimohd

New Member
Joined
Nov 22, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I'm building a userform in Excel using VBA, and I want to implement data validation for a textbox. How can I ensure that users enter only numeric values and display an error message if they input non-numeric characters? Additionally, is there a way to limit the range of acceptable values?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Easy enough to validate textbox data using vba if one knows exactly what is allowed and what is not.
Assuming 456 or 45.6 is allowed but
- 456a is not and "45 6" is not and 4,500 is not (because of the comma) then perhaps:
VBA Code:
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Select Case KeyCode
    'if true, do nothing
    Case vbKey0 To vbKey9, vbKeyBack, vbKeyDecimal, vbKeyTab, vbKeyBack, vbKeyNumpad0 To vbKeyNumpad9, 190
   
    'if true, cancel key press
    Case Else
    KeyCode = False
End Select

End Sub
Otherwise needs editing to allow comma or perhaps other characters you have in mind. Substitute your textbox control name.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Ensure that users enter only numeric values into TextBox
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top