Background:
Since recently I came to know about a secret named range that gets created through using AutoFilter
. Usually (if not always) this is called "_FilterDatabase".
I thought about playing around with this a little, but then got stuck on the Range
object it refers to. Let me explain with the example below:
Test data:
| Header1 | Header2 |
|---------|---------|
| 50 | |
| 10 | |
| 30 | |
| 40 | |
| 20 | |
Test Code:
Sub Test()
With Sheet1
.Range("A1:B1").AutoFilter 1, ">40"
Dim rng As Range: Set rng = .Range("_FilterDatabase")
If rng.SpecialCells(12).Count > 2 Then
rng.Columns(2).Offset(1).Resize(rng.Rows.Count - 1, 1).Value = "Check"
End If
.Range("A1:B1").AutoFilter
End With
End Sub
With no results
Issue:
If I would run the macro above there would be no results.
Question:
The issue is resolved using .ShowAllData
method instead of .AutoFilter
and running the code twice:
Sub Test()
With Sheet1
.Range("A1:B1").AutoFilter 1, ">30"
Dim rng As Range: Set rng = .Range("_FilterDatabase")
If rng.SpecialCells(12).Count > 2 Then
rng.Columns(2).Offset(1).Resize(rng.Rows.Count - 1, 1).Value = "Check"
End If
.ShowAllData
End With
End Sub
However, .AutoFilter
clears the filter and removes it off from our range. In both cases the secret named range will remain in the Formulas
tab under names manager.
Does someone have any idea why ShowAllData
does affect the returned named range on the second run?
AutoFilter
when you're done. – Huge.Range("_FilterDatabase")
is being set when the filter is applied. Removing theAutoFilter
seems to have no effect . – FabriAutoFilter
quest =) – Qoph