选中时,VBA更改复选框填充颜色

时间:2013-11-21 22:40:06

标签: forms excel-vba checkbox vba excel

我有一个多工作表工作簿,其中包含许多表单控件(非ActiveX)复选框。我正在寻找一些VBA,它会在选中时更改所有复选框的背景(填充)颜色(True)。我见过单个复选框的代码,但不是多个/所有框。我知道VBA足够危险,但我要学习任何新的东西。

1 个答案:

答案 0 :(得分:1)

继我的评论之后,请参阅此示例。

CheckBox3位于Sheet1CheckBox2位于Sheet2

Sub CheckBox3_Click()
    Dim actvWs As Worksheet
    Dim CB As String

    Set actvWs = ActiveSheet
    CB = Application.Caller

    ColorCB actvWs, CB

    '
    '~~> Rest of the code
    '
End Sub

Sub CheckBox2_Click()
    Dim actvWs As Worksheet
    Dim CB As String

    Set actvWs = ActiveSheet
    CB = Application.Caller

    ColorCB actvWs, CB

    '
    '~~> Rest of the code
    '
End Sub

Sub ColorCB(ws As Worksheet, Cbox As String)
    With ws.Shapes(Cbox)
        '~~> 13 is for Yellow, 1 is for White
        .Fill.ForeColor.SchemeColor = IIf(.ControlFormat.Value = xlOn, 13, 1)
        .Fill.Visible = msoTrue
    End With
End Sub

如果所有复选框的_Click()都在同一个模块中,那么您可以在模块顶部移动这两行,这样就不必在每个Sub中声明它们。

Dim actvWs As Worksheet
Dim CB As String