Hello all,
I hope this finds you all healthy and safe!
I have a Userform where I have populated some textboxes in Design and others dynamically. For the textboxes that I have created dynamically, I need to populate the Change event with code to run a routine in a module.
I'm creating the Textboxes in a Sub called CreateControl like this:
From what I can gather, I created a Class Module with the following:
I don't throw an error but it never reaches the Class Module to create the Change Event so the sub is never executed. I do realize that I created the textbox as Could someone help me? I've created textboxes dynamically a few different ways but I've never tried to add the event and I have no idea how I should even be thinking about it. Any assistance is greatly appreciated!
I hope this finds you all healthy and safe!
I have a Userform where I have populated some textboxes in Design and others dynamically. For the textboxes that I have created dynamically, I need to populate the Change event with code to run a routine in a module.
I'm creating the Textboxes in a Sub called CreateControl like this:
VBA Code:
Public newTxtAssigned As MSForms.TextBox
Sub CreateControl()
Dim newLblMain, newLblMinutes As MSForms.Label
Dim newTxtMPA, newTxtMax, newTextA2A As MSForms.Control
Dim i As Integer, TopAmt
Dim sht As Worksheet
Dim LastRow As Long
Dim NumberOfLabels As Long
Dim StartingRow As Long
Dim MaxControls As Long
Dim objMyEventClass As New Class1
Set sht = ThisWorkbook.Worksheets("Pivot")
LastRow = sht.Range("ActionList").Rows.Count + 2 'Adding two rows to account for the filters in the pivot
NumberOfLabels = LastRow - 3
StartingRow = 4
'Number of controls on the userform
MaxControls = 16
Call mdlControlActions.DeleteControls
'Sets the Top location for the first control after the 4 static controls. All others will be calculated from this one.
TopAmt = 242
For i = 0 To NumberOfLabels - 1
Set newLblMain = usrProductionReporting.Controls.Add("Forms.Label.1")
With newLblMain
.Name = i & "lblMain" & sht.Range("A" & StartingRow + i).Value
.Top = TopAmt + 6
.Visible = True
.Caption = sht.Range("A" & StartingRow + i).Value & ":"
.Tag = "LabelMain"
End With
Set newTxtAssigned = usrProductionReporting.Controls.Add(bstrProgID:="Forms.Textbox.1", Name:="Textbox" & i)
With newTxtAssigned
.Name = i & "txtAssigned" & sht.Range("A" & StartingRow + i).Value
.Top = TopAmt
.Visible = True
.Tag = "AssignedActions"
.Value = 0
Set objMyEventClass.tbEvents = newTxtAssigned
End With
Set newTxtMPA = usrProductionReporting.Controls.Add(bstrProgID:="Forms.Textbox.1", Name:="Textbox" & i)
With newTxtMPA
.Name = i & "txtMPA" & sht.Range("A" & StartingRow + i).Value
.Value = sht.Range("B" & StartingRow + i).Value
.Top = TopAmt + 6
.Visible = True
.Tag = "MPA"
End With
Set newLblMinutes = usrProductionReporting.Controls.Add("Forms.Label.1")
With newLblMinutes
.Name = i & "lblMPA" & sht.Range("A" & StartingRow + i).Value
.Top = TopAmt + 8
.Visible = True
.Caption = "minutes per action"
.Tag = "LabelMPA"
End With
Set newTxtMax = usrProductionReporting.Controls.Add(bstrProgID:="Forms.Textbox.1", Name:="Textbox" & i)
With newTxtMax
.Name = i & "txtMaxActions" & sht.Range("A" & StartingRow + i).Value
.Top = TopAmt
.Visible = True
.Tag = "MaxActions"
End With
Set newTextA2A = usrProductionReporting.Controls.Add(bstrProgID:="Forms.Textbox.1", Name:="Textbox" & i)
With newTextA2A
.Name = i & "txtAvailAssign" & sht.Range("A" & StartingRow + i).Value
.Top = TopAmt
.Visible = True
.Tag = "AvailableToAssign"
End With
TopAmt = TopAmt + newTxtAssigned.Height
Next
Call mdlControlActions.GlobalControlProperties
End Sub
From what I can gather, I created a Class Module with the following:
VBA Code:
Public WithEvents tbEvents As MSForms.TextBox
Private Sub tbEvents_Change()
mdlCalculations.DynamicMinutes
End Sub
I don't throw an error but it never reaches the Class Module to create the Change Event so the sub is never executed. I do realize that I created the textbox as Could someone help me? I've created textboxes dynamically a few different ways but I've never tried to add the event and I have no idea how I should even be thinking about it. Any assistance is greatly appreciated!