vendredi 21 juillet 2017

VBA obtaining checkbox values from userform

I'm having trouble obtaining values from checkboxes of a userform. The problem I have is that the userform creates a variable number of checkboxes based on a value from a sheet. The code for this:

Private Sub UserForm_Initialize()

Dim LastRowUserform4 As Long
Dim iUserform4 As Long
Dim TellerUserform4 As Long
Dim chkBox As MSForms.CheckBox

TellerUserform4 = 1
LastRowUserform4 = Worksheets("Overzicht orders").Cells(Rows.Count, 1).End(xlUp).Row

For iUserform4 = 1 To LastRowUserform4
    If Worksheets("Overzicht orders").Cells(iUserform4, 1).Value = Worksheets("Overzicht orders").Range("S1").Value Then
        Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & TellerUserform4)
        chkBox.Caption = Worksheets("Overzicht orders").Cells(iUserform4, 9).Value
        chkBox.Left = 5
        chkBox.Top = 25 + ((TellerUserform4 - 1) * 20)
        TellerUserform4 = TellerUserform4 + 1
    End If
Next iUserform4

End Sub

So this creates a number of checkboxes named CheckBox_1, CheckBox_2 etc. The problem is when I try to get the value for CheckBox_1 in the module, CheckBox_1 has not yet been created so I'm not able to use it.

Dim x as String
With UserForm4
     .Show
     x = .CheckBox_1
     MsgBox (x)
     End
End With




Aucun commentaire:

Enregistrer un commentaire