【问题标题】:Excel VBA combining results from multiple checkboxes into single worksheet cellExcel VBA 将多个复选框的结果组合到单个工作表单元格中
【发布时间】:2017-11-03 09:01:48
【问题描述】:

我有一个包含 20 个复选框的用户窗体框架,每个复选框代表不同颜色的衬衫。

当用户选择复选框时,我希望结果(标题,不是真/假)最终出现在活动行的单个单元格(第 8 列)中,用逗号分隔。

我是 VBA 编码和即时学习的新手 - 谁能帮忙?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    Edit1:删除_If statement 的续行。

    创建一个子例程来检查所有Checkboxes 并将数据传递给选定的单元格。
    尝试这样的操作:

    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
    

    那么你需要做的就是在你的所有CheckBox_Click事件中调用这个子例程。

    Private Sub CheckBox1_Click()
    
        GetSelection
    
    End Sub
    

    【讨论】:

    • 这几乎让我成功了,非常感谢!我刚刚在If cbk Then _ 收到语法错误,我敢肯定答案一定是盯着我的脸!!
    • @hadtobe 不确定你是什么 sysntax 错误,让我运行它,它很好。尽管如此,您可以尝试填写If statement
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2018-06-25
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多