Excel工作表中的Activex复选框变量名称

时间:2018-01-11 17:37:55

标签: excel-vba vba excel

首先,我有两个名为" Speed"和"状态"和5个activex复选框放在工作表"状态"。初始条件是单元格颜色白色,所有复选框都设置为True。基于来自" Speed"的信息,我希望自动重新检查复选框。如果未选中,则某些单元格颜色将更改为工作表中的黑色"状态"。

换句话说:如果" Speed"小于60或不同于字母C或不同于字母I,特定单元格在"状态"将继续为白色(复选框不会被更改并被选中)。如果用户手动取消选中,则单元格将变为黑色。

剪切的代码无法正常工作。没有循环。 [包含更正!]

Sub ButtonTEST()

Dim x, a As Integer
Dim obj As Object
Dim var As String

a = 1
x = 2

var = "CheckBox" & a  '<----- Correction: this line should go to [1]

Set obj = Sheets("Status").OLEObjects(var) '<----- Correction: this line should go to [2]

 For a = 1 To 5

     ' [1] insert here first
     ' [2] insert here in this order

        If (Sheets("Speed").Range("A" & x).Value >= 60) Or _
           (Sheets("Speed").Range("B" & x).Value = "I") Or _
           (Sheets("Speed").Range("C" & x).Value = "C") Then

            obj.Object.Value = False

        Else

           obj.Object.Value = True

        End If

        a = a + 1     '<----Correction : this line should be deleted
        x = x + 1
 Next a

End Sub

以下代码位于工作表&#34;状态&#34;,其中所有activex复选框都

Private Sub CheckBox1_Click()

With Sheets("Status")
            .Activate

    If ActiveSheet.CheckBox1.Value = True Then

        .Range("B10").Select
            With Selection.Interior
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

   Else

            .Range("B10").Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

    End If
End With

End Sub

Private Sub CheckBox2_Click()

With Sheets("Status")
          .Activate

    If ActiveSheet.CheckBox2.Value = True Then

            .Range("B11").Select
            With Selection.Interior
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

    Else

            .Range("B11").Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
    End If
 End With

End Sub

Private Sub CheckBox3_Click()

With Sheets("Status")
         .Activate

    If ActiveSheet.CheckBox3.Value = True Then

            .Range("B12").Select
            With Selection.Interior
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

    Else

            .Range("B12").Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

    End If
 End With

End Sub
Private Sub CheckBox4_Click()

 With Sheets("Status")
            .Activate

    If ActiveSheet.CheckBox4.Value = True Then


            .Range("B13").Select
            With Selection.Interior
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
    Else
            .Range("B13").Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
    End If
End With

End Sub
Private Sub CheckBox5_Click()

With Sheets("Status")
            .Activate

    If ActiveSheet.CheckBox5.Value = True Then

            .Range("B14").Select
            With Selection.Interior
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

    Else

            .Range("B14").Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

    End If
End With

End Sub

0 个答案:

没有答案