Hello JAL_0110,
Welcome to the Forum!
This can be done easily using a technique known as "Subclassing". This allows for objects to be grouped together and share common event routines.
Before you proceed, have your workbook open and the VB Editor activated. To activate the VB Editor, press the keys ALT+F11.
Adding the Class Module and Code- To activate the VB Editor, press the keys ALT+F11.
- Copy the Claas Code below using Ctrl+C.
- Add a new Class Module to your VBA project. Press the keys [b]ALT+I[/] to display the Insert menu. Press C key.
- Paste the code into the module using Ctrl+V.
Class Module Code
Public WithEvents Control As MSForms.TextBox
Private Sub Control_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
End If
End Sub
Additional UserForm Code
Add the code below to the very top of your UserForm...
Dim TextBoxes() As Variant
Add the code below to the UserForm_Initialize() Event Module...
Private Sub UserForm_Initialize()
Dim TB As Object
ReDim TextBoxes(1 To 167)
For i = 1 To 167
Set TB = New clsTextBox
Set TB.Control = Me.Controls("Input" & i)
Set TextBoxes(i) = TB
Next i
End Sub
If you have any problems, post your workbook and I will fix any problems you are having.
Bookmarks