0
我正在查看VBA中的各種選項以找出啓用了過濾器的工作表中的行號。Excel VBA:找出哪一行啓用了過濾器
If ThisWorkbook.Sheets(1).AutoFilterMode = True Then
上面的代碼行只檢查工作表是否包含過濾器,但我需要知道哪些行號上有過濾器。
我正在查看VBA中的各種選項以找出啓用了過濾器的工作表中的行號。Excel VBA:找出哪一行啓用了過濾器
If ThisWorkbook.Sheets(1).AutoFilterMode = True Then
上面的代碼行只檢查工作表是否包含過濾器,但我需要知道哪些行號上有過濾器。
這是一個直接回答你的問題:
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
這是如何通過過濾器進行迭代。您要檢查的財產是過濾器。對
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
這應該做你的工作。
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
爲什麼你需要迭代'每個'循環找出自動過濾器? – 2014-10-19 15:22:55
我認爲他對哪些過濾器感興趣。每行可以有一些過濾器和一些關閉。不清楚他是否想遍歷行列來找到它們。 – anefeletos 2014-10-19 15:28:25
一個工作表上最多可以應用一個過濾器。所以ThisWorkbook.Sheets(1).AutoFilter.range將完成這項工作。 – 2014-10-19 15:31:09