VBA - Numeric-only Textbox

Flashbond

New Member
Joined
Mar 29, 2024
Messages
2
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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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,215,093
Messages
6,123,066
Members
449,090
Latest member
fragment

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