I have the following code that creates a list of checkbox items on my userform based on cell values on a worksheet. How can I set each of these checkboxes to default as true? If possible, I'm also looking for a way to set the size of the userform based on the number of checkboxes created. For example, the list of checkboxes to be created is dynamic, so sometimes there may be 5 or 6, other times there are 10-11. If there are more than 10, I'd like it to put the checkboxes in two columns and resize the userform accordingly.
Thanks for any time/help/input!
Code:
Private Sub UserForm_Initialize()
Dim curColumn As Long
Dim LastRow As Long
Dim i As Long
Dim chkBox As MSForms.CheckBox
curColumn = 1 'Set your column index here
LastRow = Worksheets("sht_data").Cells(Rows.Count, curColumn).End(xlUp).Row
For i = 5 To LastRow
Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkBox.Caption = Worksheets("sht_data").Cells(i, curColumn).Value
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
Next i
End Sub
Thanks for any time/help/input!