简体   繁体   中英

Excel VBA combining results from multiple checkboxes into single worksheet cell

I have a Userform frame containing 20 checkboxes, each checkbox represents a different coloured shirt.

When a user makes their checkbox selection/s, I'd like the results (the caption, not true/false) to end up in a single cell (column 8) of the active row, separated by a comma.

I'm a newbie at VBA coding and learning on the fly - can anyone please help?

Edit1: Removed line continuation _ on If statement .

Create a sub routine to check all the Checkboxes and pass the data to the selected cell.
Try something like this:

Private Sub GetSelection()

    Dim c As MSForms.Control
    Dim cbk As MSForms.CheckBox
    Dim sel As String


    For Each c In Me.Controls '/* Iterate all controls */
        If TypeOf c Is MSForms.CheckBox Then '/* check if it is a checkbox */
            Set cbk = c '/* Set as Checkbox to get the caption property */
            If cbk Then '/* Check value, get caption if true */
                If sel = "" Then 
                    sel = cbk.Caption 
                Else 
                    sel = sel & "," & cbk.Caption
                End If
            End If
        End If
    Next

    '/* Transfer currently selected values in desired cell */
    Sheet1.Range("H" & ActiveCell.Row).Value2 = sel '/* Used H for column 8 */

End Sub

Then all you need to do is call this sub routine in all your CheckBox_Click event.

Private Sub CheckBox1_Click()

    GetSelection

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM