VBA - Numeric-only Textbox

Flashbond

New Member
Joined
Mar 29, 2024
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Here is my version of numeric-only textbox sample. I hope it helps:

VBA Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  If Not (KeyAscii >= 48 And KeyAscii <= 57) Then
    If KeyAscii = 46 Or KeyAscii = 44 Then
      If TextBox1.Text = "" Then TextBox1.Text = 0
      KeyAscii = Asc(Application.International(xlDecimalSeparator))
      If InStr(TextBox1.Text, Application.International(xlDecimalSeparator)) Then KeyAscii = 0
    Else
      KeyAscii = 0
    End If
  End If
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
It works when your typing. Blocks all non-numeric symbols, if I get the idea correctly.
But it does not forbid pasting any text, or placing text in by code.
1711720529371.png

So you need to enhance it further to be reliable, or double check the value before using it elsewhere.
If you just want to be sure that you will get a number out of the textbox (and if it is not on a complex userform) you can use:
VBA Code:
x=Application.InputBox("type a number",,,,,,,1)
 
Upvote 0
Oh, you are right. I just thought the user has a good will. It was only to prevent mistakes. A solid solution could be:
VBA Code:
Dim myText As String
Private Sub TextBox1_Change()
  If Not IsNumeric(TextBox1.Text) Then TextBox1.Text = myText
End Sub
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  myText = TextBox1.Text
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  If Not (KeyAscii >= 48 And KeyAscii <= 57) Then
    If KeyAscii = 46 Or KeyAscii = 44 Then
      If TextBox1.Text = "" Then TextBox1.Text = 0
      KeyAscii = Asc(Application.International(xlDecimalSeparator))
      If InStr(TextBox1.Text, Application.International(xlDecimalSeparator)) Then KeyAscii = 0
    Else
      KeyAscii = 0
    End If
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,224
Members
453,025
Latest member
Hannah_Pham93

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