keep header row in advanced filter

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,005
Office Version
  1. 365
Platform
  1. Windows
Folks, repurposing an old macro and need some help. The macro filters a range of data based upon the value searched for by the user. The search term is typed into A2. B2 contains a formula to add wildcards to the value at A2. ="*"&A2&"*" so if I am looking for "life" in the range, the criteria to search becomes *life* and the results listed would be life, proliferate, lifesavings, lifeguard, midlife, lowlife.... etc


The filter macro, which picks up the search term at B2 (named range "Criteria") is simple:

Code:
Sub Filter()
'///filter list in place using A2 as criteria

Dim s As Worksheet

On Error Resume Next

Set s = Worksheets("Finder")
s.Activate
With ActiveSheet
.Unprotect ("word")
.ShowAllData


Range("a4").CurrentRegion.AdvancedFilter _
    Action:=xlFilterInPlace, _
    CriteriaRange:=Range("Criteria")

.Protect ("word")
End With

End Sub

How do i keep the header row (row 4) visible and also display the filtered data set?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Do you have anything in row 3 that would be throwing your .CurrentRegion out?

This code:
VBA Code:
Sub AdvancedFilter()
    Dim Criteria As Range, RngData As Range
    
    With Worksheets("Finder")
        .Unprotect "word"
        Set Criteria = .Range("B1:B2")
        Set RngData = .Range("A4").CurrentRegion
        RngData.AdvancedFilter xlFilterInPlace, Criteria
        .Protect "word"
    End With

End Sub

Turns this:
test.xlsm
AB
1Header1
2life*life*
3
4Header1Header2
5lifedata
6life savingsdata
7proliferatedata
8datadata
9datadata
10datadata
11lifedata
12life savingsdata
13proliferatedata
14datadata
15
Finder
Cell Formulas
RangeFormula
B2B2="*"&A2&"*"


Into this:
test.xlsm
AB
1Header1
2life*life*
3
4Header1Header2
5lifedata
6life savingsdata
7proliferatedata
11lifedata
12life savingsdata
13proliferatedata
15
Finder


Is that what you want?
 
Upvote 0
Solution
Do you have anything in row 3 that would be throwing your .CurrentRegion out?

This code:
VBA Code:
Sub AdvancedFilter()
    Dim Criteria As Range, RngData As Range
   
    With Worksheets("Finder")
        .Unprotect "word"
        Set Criteria = .Range("B1:B2")
        Set RngData = .Range("A4").CurrentRegion
        RngData.AdvancedFilter xlFilterInPlace, Criteria
        .Protect "word"
    End With

End Sub

Turns this:
test.xlsm
AB
1Header1
2life*life*
3
4Header1Header2
5lifedata
6life savingsdata
7proliferatedata
8datadata
9datadata
10datadata
11lifedata
12life savingsdata
13proliferatedata
14datadata
15
Finder
Cell Formulas
RangeFormula
B2B2="*"&A2&"*"


Into this:
test.xlsm
AB
1Header1
2life*life*
3
4Header1Header2
5lifedata
6life savingsdata
7proliferatedata
11lifedata
12life savingsdata
13proliferatedata
15
Finder


Is that what you want?
that works for me. Thanks Kevin.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,214
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top