How to make checkboxes (form or Active X) and return all checked names into an array?

wiseone

Board Regular
Joined
Mar 14, 2015
Messages
144
Hi all,

I have a bit of code which extracts data from source files. I'd like to be able to select checkboxes to extract the data from several files at once by choosing checkboxes.

Each checkbox will correspond to a file name.

I prefer form control because it seems easier to setup, but I'm not opposed to activeX if needed.

How do I read all the names of the checked boxes into an array when a macro is executed?

Example:

Checkboxes:

Name 1 - Checked
Name 2 - Checked
Name 3 - Not Checked
Name 4 - Checked
Name 5 - Not Checked

The result would be:

Dim cbSelections as Variant
cbSelections = array("Name 1", "Name 2", "Name 4")

Thanks in advance!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
The following macro assumes that the sheet containing the checkboxes is the active sheet...

Code:
Sub test()

    Dim aCheckBoxNames() As String
    Dim oChBx As CheckBox
    Dim ChBxCnt As Long
    Dim i As Long
    
    With ActiveSheet
        ReDim aCheckBoxNames(1 To .CheckBoxes.Count)
        ChBxCnt = 0
        For Each oChBx In .CheckBoxes
            If oChBx.Value = xlOn Then
                ChBxCnt = ChBxCnt + 1
                aCheckBoxNames(ChBxCnt) = oChBx.Name
            End If
        Next oChBx
        ReDim Preserve aCheckBoxNames(1 To ChBxCnt)
    End With
    
    For i = LBound(aCheckBoxNames) To UBound(aCheckBoxNames)
        Debug.Print aCheckBoxNames(i)
    Next i
    
End Sub

Hope this helps!
 
Upvote 0
thank you! Is this for form or active x? (Sorry new at using anything other than a button to start macros)......and if there are no checkboxes checked...will it cause an error?
 
Upvote 0
Hi...also I just tried this and it did not work....it captured the checkbox name, but not the text associated with the checkbox that the user sees.....do you know how to capture that instead?
 
Upvote 0
thank you! Is this for form or active x? (Sorry new at using anything other than a button to start macros)......

It's for checkboxes from Form controls...

...and if there are no checkboxes checked...will it cause an error?

Yes, sorry, I've amended the code to deal with it, and I've amended it to capture the caption instead of the name...

Code:
Sub test()

    Dim aCheckBoxNames() As String
    Dim oChBx As CheckBox
    Dim ChBxCnt As Long
    Dim i As Long
    
    With ActiveSheet
        ReDim aCheckBoxNames(1 To .CheckBoxes.Count)
        ChBxCnt = 0
        For Each oChBx In .CheckBoxes
            If oChBx.Value = xlOn Then
                ChBxCnt = ChBxCnt + 1
                aCheckBoxNames(ChBxCnt) = oChBx.Caption
            End If
        Next oChBx
    End With
    
    If ChBxCnt > 0 Then
        ReDim Preserve aCheckBoxNames(1 To ChBxCnt)
        For i = LBound(aCheckBoxNames) To UBound(aCheckBoxNames)
            Debug.Print aCheckBoxNames(i)
        Next i
    Else
        MsgBox "No checkboxes were checked...", vbInformation
    End If
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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