VBA涉及一张纸上的多个复选框

时间:2014-06-16 17:54:38

标签: vba excel-vba checkbox excel

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

设置方式:

  • 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

1 个答案:

答案 0 :(得分:0)

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