Looping through Checkboxes in Userform to gather Caption names

geekgirl613

New Member
Joined
Sep 10, 2014
Messages
6
Hello Everyone, this is my first time on this site, so I hope I provide enough detailed information. I am working on a vba userform that I need to check to see if checkboxes are checked and I need to gather the caption name, if they are. I am having a difficult time with iterating through the checkboxes. I have figured out how to do it one by one, but I have quite a few checkboxes to go through. Thanks!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I have just finished a battleship userform so I could learn ALL about checkboxes/option buttons/text boxes/ what have you. So here's how I did it:

Code:
    For Each myFrame In Me.Controls        
        If Left(myFrame.Name, 8) = "CheckBox" Then
            myFrame.Enabled = True
            myFrame.BackColor = &H8000000F
        End If
    Next

Of course my check boxes were all CheckBox1, CheckBox2.....CheckBox100
 
Upvote 0
My question here is how would I use this loop to capture each caption name because there is no index. Also, is there a book or source that is really good at describing all of the functions of checkboxes.
I have just finished a battleship userform so I could learn ALL about checkboxes/option buttons/text boxes/ what have you. So here's how I did it:

Code:
    For Each myFrame In Me.Controls        
        If Left(myFrame.Name, 8) = "CheckBox" Then
            myFrame.Enabled = True
            myFrame.BackColor = &H8000000F
        End If
    Next

Of course my check boxes were all CheckBox1, CheckBox2.....CheckBox100
 
Upvote 0
What are your checkbox names and where do you want to store them? I don't mean the caption names, I mean the actual names?
 
Upvote 0
Simple example using TypeName so you don't have to rely on control names:

Code:
    Dim ctl                   As MSForms.Control
    Dim n                     As Long
    Dim myArray()             As String

    For Each ctl In Me.Controls
        If TypeName(ctl) = "CheckBox" Then
            If ctl.Value = True Then
                ReDim Preserve myArray(n)
                myArray(n) = ctl.Caption
                n = n + 1
            End If
        End If
    Next ctl

    MsgBox myArray(0)
 
Upvote 0
Thank you so much for your help, but I have one more question. Can I create a sub to start at ANY click event (mainly for any checkbox) as I do not know what the user will click first?
Simple example using TypeName so you don't have to rely on control names:

Code:
    Dim ctl                   As MSForms.Control
    Dim n                     As Long
    Dim myArray()             As String

    For Each ctl In Me.Controls
        If TypeName(ctl) = "CheckBox" Then
            If ctl.Value = True Then
                ReDim Preserve myArray(n)
                myArray(n) = ctl.Caption
                n = n + 1
            End If
        End If
    Next ctl

    MsgBox myArray(0)
 
Upvote 0
Thank you so much for your help, but I have one more question. Can I create a sub to start at ANY click event (mainly for any checkbox) as I do not know what the user will click first?

It's probably smarter to have a button at the end and collect it all once that button is pressed. That way you only have one set of looping through the checkboxes. Plus, you won't be overwriting arrays when the second and third check boxes are checked.
 
Upvote 0
I agree with NeonRedSharpie but for reference, you can handle the click event of every checkbox using a class module. There are many examples on this forum - for example post #4 on this thread.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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