Hi
I have a userform with 4 textbox and a function to force users input only number in it.
As code below shows, I have to right this function for each textbox keypress event.
Now I need to know is this possible to have a loop through userform textboxes to do it?
I have a userform with 4 textbox and a function to force users input only number in it.
As code below shows, I have to right this function for each textbox keypress event.
Now I need to know is this possible to have a loop through userform textboxes to do it?
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
OnlyNumber KeyAscii, Me.TextBox1
End Sub
Function OnlyNumber(key As MSForms.ReturnInteger, tb As MSForms.TextBox)
If key > Asc("9") Or key < Asc("0") Then
If key = Asc("-") Then
If InStr(1, tb.Text, "-") > 0 Or _
tb.SelStart > 0 Then key = 0
ElseIf key = Asc(".") Then
If InStr(1, tb.Text, ".") > 0 Then key = 0
Else
key = 0
End If
End If
End Function