简体   繁体   English

Excel VBA将多个复选框的结果合并到单个工作表单元格中

[英]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. 我有一个包含20个复选框的Userform框架,每个复选框代表一个不同颜色的衬衫。

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. 当用户进行复选框选择时,我希望结果(标题,而不是真/假)最终在活动行的单个单元格(第8列)中,以逗号分隔。

I'm a newbie at VBA coding and learning on the fly - can anyone please help? 我是VBA编码和动态学习的新手 - 有人可以帮忙吗?

Edit1: Removed line continuation _ on If statement . Edit1:If statement删除了行继续_

Create a sub routine to check all the Checkboxes and pass the data to the selected cell. 创建子例程以检查所有Checkboxes并将数据传递到所选单元格。
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. 然后,您需要做的就是在所有CheckBox_Click事件中调用此子例程。

Private Sub CheckBox1_Click()

    GetSelection

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将多个工作簿中的数据合并到一个工作表中 - Combining data from multiple workbooks into a single worksheet 复选框,用于Excel中单个单元格中的多个值 - Checkboxes for multiple values in a single cell in Excel Excel VBA-合并多个工作簿中的工作表并重命名新的工作表选项卡 - Excel VBA - combining worksheets from multiple workbooks and rename new worksheet tab 使用VBA将单个单元格从工作表复制到另一个工作表 - Copy single cell from worksheet to another worksheet using VBA 如何在多次试验中在 Excel 工作表中重新计算(F9/模拟)时记录单个单元格的结果 - How to log results from a single cell as I recalculate (F9/simulation) in a Excel worksheet over many trials 将 VBA 中多行的信息组合到新工作表中 - Combining the information from multiple rows in VBA onto a new worksheet 使用VBA将特定文件夹中的多个Excel工作簿导入到单个工作表中 - Import Multiple Excel Workbook From Specific Folder Into Single Worksheet Using VBA 将多个工作表中的数据编译到单个工作簿中的主工作表 - VBA Excel 宏 - Compiling data from multiple worksheets to a master worksheet in a single workbook - VBA Excel Macro Excel中的VBA脚本以填充工作表中单元格中的.from字段 - VBA script in Excel to populate the .from field from a cell in the worksheet 合并两个工作表更改事件Excel VBA - Combining two worksheet change events excel vba
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM