I have a userform with various checkboxes. Each checkbox when selected opens up a new userform with a new set of checkbox, textbox and spinbutton controls based on a column in a worksheet. This column will change and thus there will be a new number of checkboxes, textbox and spinbutton controls every time the macro runs.
I need to use the spinbutton_change()
event to set the value of the textbox. Since I don't know the number of spinbuttons that will exist at a given time how can I code this so each spinbutton updates its respective textbox?
Private Sub UserForm_Activate()
Dim NewChkBx As MSForms.CheckBox
Dim NewTxtBx As MSForms.TextBox
Dim NewSpinButton As MSForms.SpinButton
Dim rngSource As Range
Dim rngCell As Range
Dim TopPos As Integer
Dim MaxWidth As Long
Sheets("Background").UsedRange.ClearContents
'Filters items in Col A which Equal "Light"
Application.ScreenUpdating = False
Worksheets("Weight_Data").Unprotect
Worksheets("Background").[a1].CurrentRegion.Offset(1).ClearContents
Worksheets("Weight_Data").Range("D1:D1000").AutoFilter 1, "Light"
Worksheets("Weight_Data").Range("A2:H1000").Copy (Worksheets("Background").Range("A65536").End(xlUp)(2))
Worksheets("Weight_Data").Range("A1").AutoFilter 'Turn Filter Off
' End of filter
' Start of creating new userform based only on checked boxes.
With Worksheets("Background")
Set rngSource = Worksheets("Background").Range("B2", .Cells(.Rows.Count, "B").End(xlUp))
End With
TopPos = 5
MaxWidth = 0
For Each rngCell In rngSource
If rngCell.Value <> "" Then
Set NewChkBx = Me.Controls.Add("Forms.CheckBox.1")
Set NewTxtBox = Me.Controls.Add("Forms.Textbox.1")
Set NewSpinButton = Me.Controls.Add("Forms.Spinbutton.1")
With NewChkBx
.Caption = rngCell.Value
.Left = 5
.Top = TopPos
.AutoSize = True
If .width > MaxWidth Then MaxWidth = .width
End With
With NewTxtBox
.Enabled = True
.Value = 0
.Enabled = False
.Text = 0
.Height = 15
.width = 20
.Left = NewChkBx.width + 20
.Top = TopPos
.AutoSize = True
If .width > MaxWidth Then MaxWidth = .width
End With
With NewSpinButton
.Value = 0
.Height = 15
.width = 10
.Left = NewTxtBox.width + NewChkBx.width + 20
.Top = TopPos
If .width > MaxWidth Then MaxWidth = .width
End With
TopPos = TopPos + 60
End If
Next rngCell
Me.width = MaxWidth + 60
Me.Height = TopPos + 25
' Worksheets("Weight_Data").Protect
Application.ScreenUpdating = True
Sheets("Background").UsedRange.ClearContents
End Sub
Here's a slightly-simplified version of your code which has some dynamic event capture built in.
Userform:
Class module "clsEvents":