I tried adapting the solution in the link below to make a collection of text boxes allow numbers only. I get no error but the class just doesn't apply to the textboxes.
Excel VBA Userform - Execute Sub when something changes
Class Module
Public WithEvents TextGroup As MSForms.TextBox
Public Property Set Control(tb As MSForms.TextBox)
Set TextGroup = tb
End Property
Private Sub TextGroup_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub
UserForm
Dim tbCollection As Collection
Private Sub UserForm_Initialize()
Dim obj As clsTextBox
Dim ctrl As Control
Set tbCollection = New Collection
tbCollection.Add Me.tbAC
tbCollection.Add Me.tbCR
tbCollection.Add Me.tbHP
For Each ctrl In tbCollection
Set obj = New clsTextBox
Set obj.Control = ctrl
Next
End Sub
Can you listen for a TextBox exit event? Similarly to how a normal TextBox event would work? E.g.
The following doesn't catch the exit event. Moreover, while I can see the .Name property of the TextBox which generated the event for MyTextBox in the locals window, I cannot access that info to determine which label to act on.
This class technique does catch some of the change events.
Class clsTextBox:
I have a series of dynamically created controls which need listeners. Code follows:
You need to put the obj objects in the collection, not the controls themselves
Untested: