Excel VBA:找出哪个行启用了过滤器

时间:2014-10-19 14:28:11

标签: excel vba

我正在查看VBA中的各种选项,以找出已启用过滤器的工作表中的行号。

If ThisWorkbook.Sheets(1).AutoFilterMode = True Then

以上行仅检查工作表是否包含过滤器,但我需要知道哪个行号有过滤器。

2 个答案:

答案 0 :(得分:0)

这是你问题的直接答案:

Function CheckWhichRowHasFilter(r As range)
For Each rowi In r.Rows' Dim rowi As range
Set pa = rowi.Parent.AutoFilter'Dim pa As AutoFilter
If pa.FilterMode = True Then
  CheckWhichRowHasFilter = pa.range.Address
    Exit For
End If
Next rowi
End Function

这是迭代过滤器的方法。您要检查的属性是Filter.On

Sub IterateThroughFilters()
Dim r As range
Set r = Selection
Dim rc As range
For Each rc In r.Columns
   If Not rc.Parent.AutoFilter Is Nothing Then
    Set currentColumnFilter = rc.Parent.AutoFilter ' Filteraddress: = currentColumnFilter.range.Address
       Dim ccf As filters
       Set ccf = currentColumnFilter.filters
         Dim cf1 As filter
         Set cf1 = ccf(1) 'onebased index
             If cf1.On Then 'Here you check if filter is on

                cfc1 = cf1.Criteria1(1)
                cfc2 = cf1.Criteria1(2)
                cfc3 = cf1.Criteria1(3)
            End If
   End If

Next rc

End Sub

答案 1 :(得分:0)

这应该可以帮到你。

 Sub test()

    Dim rngRange        As range

    If ThisWorkbook.Sheets(1).AutoFilterMode = True Then
        Set rngRange = ThisWorkbook.Sheets(1).AutoFilter.range
        MsgBox "Address of Filter: " & rngRange.Address & Chr(10) _
                & "Row Number is: " & rngRange.Row, vbOKOnly
    End If

    Set rngRange = Nothing

End Sub