Array of Checkboxes

magnamundian

New Member
Joined
May 26, 2009
Messages
13
Hi

I'm looking to put together a VBA sub/module which, using the following variables:

Dim iOptions as Integer
iOptions = {spreadsheet refererence}
ReDim sNames(1 to iOptions) as string array
ReDim bSelected(0 to iOptions) as boolean array

Would achieve the following:

Populate a UserForm with {iOptions + 1} Checkboxes and an OK button.
Label the first CheckBox as "Select All" and all subsequent CheckBoxes according to the labels found in the {sNames} array.

When user ticks whatever number of checkboxes they want, the variable {bSelected} would be populated with either True or False based on whether the Checkbox was ticked (True) or not (False) with bSelected(0) being the "Select All" checkbox and all subsequent checkboxes being number 1, 2, 3... etc

This would need to be dynamic, i.e. the number of boxes required would go up or down.

Any thoughts?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Have you considerd a listbox?

They can show a checkbox for each item and might be a little easier to work with than multple dynamic checkboxes.
 
Upvote 0
Well you would just populate the listbox with the options and set a couple of properties to show the list with a checkbox next to each item.

How to populate really depends on where the data for the options is located.

For example if it's on a worksheet you could simply set the List property of the listbox
Code:
Listbox1.List = Range("A1:A10").Value
Though in your case, with the Select All option, you might want to use AddItem.
Code:
Set rng =Range("A1:A10")
 
Listbox1.Additem "Select All"
 
For Each cl In rng
      Listbox1.AddItem = cl.Value
Next cl

When it comes to checking what's been checked you can just loop through the items in the listbox and check there Selected property.

Similarly if Select All is checked then you can loop through and set the Selected property.

Oh, forgot to ask - where would you want to do this?

On a worksheet? A userform?
 
Upvote 0
Userform, and I think that helps me a lot, sometimes with a new thing it's just finding the basic starting point of how to do something in vba.
 
Upvote 0
If you want to use a Listview then you can have checkBoxes on the list - it is more coding but very flexible - The code below give you an idea of how this works - Just insert a ListView on your userform

Code:
Private Sub UserForm_Activate()
    Dim list_item As ListItem
    With ListView1
        .MultiSelect = True
        .CheckBoxes = True
        .BackColor = &H80FFFF
        .ListItems.Clear
        .ColumnHeaders.Add , , "File Names", 120
        .ColumnHeaders.Add , , "What/ever", 85
        Set list_item = .ListItems.Add(1, , "File_1")
        list_item.SubItems(1) = "Entry one"
        Set list_item = .ListItems.Add(1, , "File_2")
        list_item.SubItems(1) = "Entry Two"
 
        .View = 3 ' you can mess with this - changes layout
 
    End With
End Sub
 
Last edited:
Upvote 0
BTW - Norie - I luv my ListViews - thanks for pointing me to ListViews - lol
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,882
Members
452,948
Latest member
Dupuhini

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