CheckBox作为条件

时间:2018-11-15 14:22:01

标签: excel vba

我正在尝试使用ActiveX复选框来过滤将要执行的代码行,但是会引发“ 400”错误,也是由于某些原因我无法取消选中这些复选框,有人可以帮忙吗?

.
.
.
If CheckBox1.Value = True Then
     Columns("A:F").AutoFilter Field:=1, Criteria1:=">11999", Operator:=xlAnd, Criteria2:="<13000"
     ActiveSheet.PrintOut
End If

If CheckBox2.Value = True Then
     Columns("A:F").AutoFilter Field:=1, Criteria1:=">12999", Operator:=xlAnd, Criteria2:="<14000"
     ActiveSheet.PrintOut
End If
.
.
.

编辑(整个代码-会有更多相同条件,只是复选框和范围不同):

Sub TISK_IV_OC()

    Sheets("TISK IV OC").Activate

    Dim ws As Worksheet
    Set ws = ActiveSheet

    If ws.Range("A1") = "" Then
        MsgBox ("Neni co tisknout › nejdrive preved data!")
        Exit Sub
    End If

    Sheets("TISK IV OC").PageSetup.CenterFooter = "&""Calibri,Bold""&18 " & "IV OC: " & Format(Date + 1, "dd.mm.yyyy")

    Application.PrintCommunication = False
    With Sheets("TISK IV OC").PageSetup
        .FitToPagesWide = 1
        .FitToPagesTall = False
    End With
    Application.PrintCommunication = True

    Dim x As Integer
    For x = 1 To 2
        Sheets("TISK IV OC").PageSetup.CenterHeader = "&""Calibri,Bold""&18 " & x & " . KOLO"

        If ws.CheckBox1.Value = True Then
            Debug.Print ws.CheckBox1.Value
            ws.Columns("A:F").AutoFilter Field:=1, Criteria1:=">11999", Operator:=xlAnd, Criteria2:="<13000"
            ws.PrintOut
        End If

        If ws.CheckBox2.Value = True Then
            Debug.Print ws.CheckBox1.Value
            ws.Columns("A:F").AutoFilter Field:=1, Criteria1:=">12999", Operator:=xlAnd, Criteria2:="<14000"
            ws.PrintOut
        End If
     Next x

    ActiveSheet.ShowAllData
    ws.Range("A1").Select

End Sub

Private Sub CheckBox1_Click()

End Sub

1 个答案:

答案 0 :(得分:0)

您需要指定工作表。最好对括号“(Name)”中的属性中显示的工作表使用代码名称。

If Sheet1.CheckBox1.Value = True Then
    Debug.Print Sheet1.CheckBox1.Value
    ActiveSheet.Columns("A:F").AutoFilter Field:=1, Criteria1:=">11999", Operator:=xlAnd, Criteria2:="<13000"
    ActiveSheet.PrintOut
End If

Using code name

https://wellsr.com/vba/2015/excel/complete-guide-to-excel-vba-ActiveX-checkboxes/#value