【问题标题】:VBA involving multiple checkboxes on one sheetVBA 在一张纸上涉及多个复选框
【发布时间】:2014-06-17 04:03:09
【问题描述】:

我目前在下面的一张表中有以下代码。

设置方式:

  • A6:A35 都根据相应行的 F 列中的复选标记填充 TRUE 或 FALSE 值。
  • 如果选中 F6,则 A6 为 TRUE .. 未选中时为 FALSE。
  • B 列的内容永远不会改变。它们只是对工作表上的行进行编号。

我现在工作的代码只是设置为在工作表的一行上工作。我的想法是先让代码工作,然后将其扩展到工作表的其余部分。基本上现在它所有的工作和功能都按照我想要的方式用于第 6 行。我希望它在从第 6 行到第 35 行的每一行上独立工作。

我的问题:我如何让它查看每一行,所以无论选中哪一行复选框,它都会经历事件循环,以及如何列出每个框的复选框名称,因为有 30 个不同的盒子?

我希望这是有道理的 .. 对不起,如果我的代码真的很丑 .. 它是从大量在线阅读中拼凑而成的,并且在第 6 行完全按照我想要的方式工作 .. 现在如果我可以让它工作从第 6 行到第 35 行的方式相同,我很高兴!

Sub Checkbox_Uncheck()

    Dim Response As VbMsgBoxResult

    If Range("A6") = True Then
        Response = MsgBox("Are you finished with the keyfob programmer?", vbQuestion + vbYesNo)

        If Response = vbNo Then
            MsgBox "Finish scheduled programming before selecting the completed checkbox!", vbInformation
            Worksheets("Sign Up Sheet").CheckBoxes("Check Box 32").Value = xlOff
        Else
            'Call Reload_financials
        End If
    Else
    End If

    Dim myLastRow As Long
    Dim i As Long

    Application.ScreenUpdating = False
    Application.Wait Now + TimeSerial(0, 0, 1)


    '   Find last row
    myLastRow = Cells(Rows.Count, "A").End(xlUp).Row

    '   Loop through range
    For i = 6 To myLastRow
        If Cells(i, "A").Value = True Then Range(Cells(i, "C"), Cells(i, "E")).ClearContents

        Worksheets("Sign Up Sheet").CheckBoxes("Check Box 32").Value = xlOff
    Next i

    Application.ScreenUpdating = True
End Sub

【问题讨论】:

  • 为什么“复选框 32”在第 6 行?你的其他 29 个复选框的名称是什么?它们的命名方式是否有任何逻辑,以便我们可以对此进行编程?如果不是,我建议将它们从 6 重命名为 35,以便它们轻松与相关行重合。
  • Checkbox_Uncheck 中,您可以使用Application.Caller 获取单击复选框的名称 - 您可以从中提取名称中的行(如果您遵循@DavidZemens 的建议)或获取@表格上复选框Shape 的987654324@(假设您的复选框完全正确地位于每一行内)。

标签: vba excel checkbox


【解决方案1】:
Sub Checkbox_Uncheck()
    Dim cbName as string, rw as long
    Dim Response As VbMsgBoxResult
    Dim sht as WorkSheet

    Set sht = ActiveSheet
    cbName = Application.Caller 'name of triggering checkbox

    'as suggested by David Z,rename your checkboxes as "cbRow_x" where x=row number
    rw = CLng(Replace(cbName,"cbRow_",""))


    If sht.Cells(rw, "A") = True Then
        Response = MsgBox("Are you finished with the keyfob programmer?", _
                     vbQuestion + vbYesNo)

        If Response = vbNo Then
            MsgBox "Finish scheduled programming before selecting" & _
                   " the completed checkbox!", vbInformation
            sht.CheckBoxes(cbName).Value = xlOff
        Else
            'Call Reload_financials
        End If
    Else
    End If

    Dim myLastRow As Long
    Dim i As Long

    Application.ScreenUpdating = False
    Application.Wait Now + TimeSerial(0, 0, 1)


    '   Find last row
    myLastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

    '   Loop through range
    For i = 6 To myLastRow
        If sht.Cells(i, "A").Value = True Then 
           sht.Range(sht.Cells(i, "C"), sht.Cells(i, "E")).ClearContents
        End If

        sht.CheckBoxes("cbRow_" & i).Value = xlOff
    Next i

    Application.ScreenUpdating = True
End Sub

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    相关资源
    最近更新 更多