PDA

View Full Version : [SOLVED:] Multiple textboxes that only accepts numeric value



cjvdg
01-24-2021, 10:12 PM
Hello! I just want to ask, is there a way for me to have multiple text boxes that only accept numeric value?

So, the picture below is from my UserForm

27796

This is the code I use for validation in which I must replicate per textbox.



Private Sub tb_mtb_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Not IsNumeric(tb_mtb.Value) Then
MsgBox "Only numbers allowed!", vbOKOnly + vbCritical, "Title"
tb_mtb.Value = ""
End If
End Sub

Private Sub tb_fil_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Not IsNumeric(tb_fil.Value) Then
MsgBox "Only numbers allowed!", vbOKOnly + vbCritical, "Title"
tb_fil.Value = ""
End If
End Sub

iivansi2
01-24-2021, 11:27 PM
Hello,

yes you can this is code i use in my userforms for numbers:


Private Sub txtPart_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
'Case Asc("-") - u can add these if you need negative numbers
'Case Asc(".") - u can add these if you need decimal numbers
Case Else
KeyAscii = 0
End Select
End Sub

KeyAscii = 0 this is used to not allow spaces.

cjvdg
01-24-2021, 11:38 PM
Hello,

yes you can this is code i use in my userforms for numbers:


Private Sub txtPart_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
'Case Asc("-") - u can add these if you need negative numbers
'Case Asc(".") - u can add these if you need decimal numbers
Case Else
KeyAscii = 0
End Select
End Sub

KeyAscii = 0 this is used to not allow spaces.

How can I apply this on my textboxes?

iivansi2
01-25-2021, 12:24 AM
Your textboxes in your userform each of them have name when you using this vb code in your userform you have to change "txtPart" with name of your textbox where u need to use numbers.

snb
01-25-2021, 02:23 AM
Is there a limited set of valid numbers ?

cjvdg
01-25-2021, 04:56 PM
Your textboxes in your userform each of them have name when you using this vb code in your userform you have to change "txtPart" with name of your textbox where u need to use numbers.

So I'm going to create a private sub with each of every textboxes, is that right?

cjvdg
01-25-2021, 04:57 PM
Is there a limited set of valid numbers ?

What do you mean by that? The value that should be in the textboxes is between 50-100.

snb
01-26-2021, 01:27 AM
In that case you'd better use a combobox filled with valid numbers:


Combobox1.list=[row(50:100)]