Set checkbox value to true

zJenkins

Board Regular
Joined
Jun 5, 2015
Messages
148
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.

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!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I just got checkboxes to set as TRUE....any help on the other part of the question would be greatly appreciated! Thanks.

Code:
For Each ctrl In Me.Controls    ctrl.Value = True
Next ctrl
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top