Sub for keypress check

Latexy

New Member
Joined
Oct 8, 2019
Messages
19
Hi!

Well i have a question for check keypress for multiple textbox with public sub.
Heres example how i have done it now for each textbox.
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9]")
End Sub
So i would like to make one sub wich to call for keypress check.

-Late
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Put the following code in userform

Code:
Dim TxtBx() As New Class1  [COLOR=#008000]'At the beginning of all the code[/COLOR]


Private Sub UserForm_Initialize()
  Dim i As Long, n As Long, ctrl As MSForms.Control
  i = 1
  For Each ctrl In Me.Controls
    If TypeName(ctrl) = "TextBox" Then
      ReDim Preserve TxtBx(i)
      Set TxtBx(i).MultTextbox = ctrl
      i = i + 1
    End If
  Next
End Sub

------------------------------------------
Insert a class module "Class1"
Put the following code in Class1

Code:
Public WithEvents MultTextbox As MSForms.TextBox    [COLOR=#008000]'At the beginning of all the code[/COLOR]


Private Sub MultTextbox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9]")
End Sub
 
Upvote 0
Thank you!
One more thing i noticed is there should be possible to write comma to make value with desimals. Can you help with it?

-Late
 
Upvote 0
Use this in class module

Code:
Public WithEvents MultTextbox As MSForms.TextBox


Private Sub MultTextbox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  If Not (KeyAscii >= 48 And KeyAscii <= 57) And KeyAscii <> 44 Then
    KeyAscii = 0
  End If
End Sub
 
Upvote 0
Thank you very much!
Nice to get help because im just beginner of coding and learning it while trying to make some simple workbooks :)

-Late
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,120
Members
453,021
Latest member
Justyna P

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