VBA:从复选框中取消隐藏包含数组中值的行

时间:2015-09-01 16:32:37

标签: excel vba excel-vba checkbox

我有三个复选框(Cat,Dog,Mouse),我在excel中有以下数据集(字母是列)。

A      B       C
Cat   Cat     Mouse
Dog   Mouse     Cat
Mouse  Mouse     Dog
Dog    Cat       Cat

我有以下代码:

Dim num As Integer

arrcriteria = Array("Cat", "Dog", "Mouse")

Set temprange = ActiveSheet.Range("A1").End(xlDown)
Set temprange = Range(Range("A2"), temprange)

For Each z In temprange    
    z.EntireRow.Hidden = True    
    For num = 1 To 3    
       If Me.Controls("Checkbox" & num) Then    
          If InStr(1, z.Value, arrcriteria(num - 1), 1) = 1 Then    
              z.EntireRow.Hidden = False    
          Elseif InStr(1, z.offset(0,1).Value, arrcriteria(num - 1), 1) = 1 Then
              z.EntireRow.Hidden = False
          Elseif InStr(1, z.offset(0,2).Value, arrcriteria(num - 1), 1) = 1 Then
              z.EntireRow.Hidden = False
          Else    
              'Remain hidden    
          End If 
       End if
   Next num

但是,我想更改代码,以便在该行中仅存在所有选定条件时隐藏行。例如,如果我选择“Cat'和' Dog'以下仅显示:

 A      B       C
Dog    Mouse   Cat
Dog    Cat     Cat

你知道怎么做吗?

提前谢谢

威尔

1 个答案:

答案 0 :(得分:0)

Dim num         As Integer
Dim RowString   As String
Dim RowHide     As Boolean
Dim Z           As Range

arrcriteria = Array("Cat", "Dog", "Mouse")

Set temprange = ActiveSheet.Range("A1").End(xlDown)
Set temprange = Range(Range("A2"), temprange)

For Each Z In temprange

    With Z
        Z.EntireRow.Hidden = False
        RowString = .Value & .Offset(0, 1).Value & .Offset(0, 2).Value
        RowHide = False

        For num = 1 To 3
            If Me.Controls("Checkbox" & num) And _
            InStr(1, RowString, arrcriteria(num - 1), 1) = 0 _
            Then RowHide = True
        Next num

        If RowHide Then Z.EntireRow.Hidden = True
    End With
Next Z