Losing One of Two Header Rows In When Autofilter Applied

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm having some issue with this code and I'm not certain where I need to make corrections.
Code:
        With ws_sdata
            If .AutoFilterMode = True Then .AutoFilterMode = False
            lrow = .Cells(.Rows.Count, "B").End(xlUp).Row
            Set filter_rng = .Range("A3:A" & lrow)
            .Range("A3").AutoFilter Field:=2, Criteria1:=inqtofind & "*"
            For y = 1 To .Range("B3").CurrentRegion.Rows.Count
                If .Cells(y, 2).EntireRow.Hidden = False Then
                    zcount = zcount + 1
                End If
            Next y
        End With

In my database, rows 1 and 2 are header rows. When I run the filter, row 2, a header row is hidden with this code.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Autofilter requires a header row. Not sure at the moment why row 2 is getting hidden but what happens when you change your code to the below.
Not sure what your 2nd part of the code is supposed to do as you aren't doing anything with zcount


VBA Code:
        With ws_sdata
            If .AutoFilterMode = True Then .AutoFilterMode = False
            lrow = .Cells(.Rows.Count, "B").End(xlUp).Row
            Set filter_rng = .Range("A2:B" & lrow)
            filter_rng.AutoFilter Field:=2, Criteria1:=inqtofind & "*"
            For y = 1 To .Range("B3").CurrentRegion.Rows.Count
                If .Cells(y, 2).EntireRow.Hidden = False Then
                    zcount = zcount + 1
                End If
            Next y
        End With
 
Upvote 0
Solution
Hi Mark858,
When this solution didn't work at first I tried a bunch of different things with this code specifically. They I decided to start further back in my code stepping through. I had another filter having had run on that same data earlier that was doing the same thing, so I changed it to reflect some of the changes you suggested. Seems to all be working now with a couple improvements from your solution. Thank you!
 
Last edited:
Upvote 0
The only issue now that I'm having, is instead of the filter being applied to row 2
Is there a header in row B2? you did change the range to row 2?
Rich (BB code):
Set filter_rng = .Range("A2:B" & lrow)
 
Upvote 0
Hi Mark! You must have answered while I was editing my post. I found that yes I had indeed had the wrong range reference.
 
Upvote 0

Forum statistics

Threads
1,223,968
Messages
6,175,677
Members
452,666
Latest member
AllexDee

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