VBA counting autofiltered rows incorrectly

baitmaster

Well-known Member
Joined
Mar 12, 2009
Messages
2,042
Hello all

I have a range of data, and have autofiltered it. I am trying to count the number of rows remaining, using
Code:
Range("rosterMatrixAll").SpecialCells(xlCellTypeVisible).Rows.Count

This usually returns the correct value, except in some scenarios, where the first 8 (or so) lines of data are hidden. In this scenario, the value returned = 1

The header row (row 1 of the range) is always visible, its where the autofilter boxes appear, and contains labels. The last row always disappears, its a dummy line to act as the bottom border of the table, and contains no data

Ive tried other things as a test; notably, I split the code, so that the visible range only is selected, and then used selection.rows.count. Im expecting to see 100 or so, because that many rows are now selected. Still I get 1

Are there any known issues that would cause this? It only seems to occur when rows 2:x are hidden by autofilter

I'm using xl2007, but not a "table", as I am trying to maintain backwards compatibility

Thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Bump...

VBA seeing only 1 visible row, if first line of data autofiltered out... Any suggestions for a better way to count # of visible rows in an autofiltered table?

The correct answer is visible next to the statusbar, can I access this?

thanks
 
Upvote 0
Thanks for your interest

That was the auotfiltered range, as described
- a rectangular block, all data (no blanks), headers on top row, with autofilter on same header row
- named range, exactly as referred to in code
- blank bottom row, to ensure calculations update when new lines added

I think I needed to refer to the Autofilter object, instead of the named range which was being autofiltered. The following link contains some autofilter counting code, which is returning the correct values. I will integrate this with my code, and hopefully that will work:
http://www.contextures.com/xlautofilter03.html#Count
 
Upvote 0
Thanks for the help so far, I have a partial fix based on the previous comments, but am still experiencing related problems, and I think the autofilter feature has a glitch in it...

I have also changed another part of my method of counting, to use subtotal(2,range("").columns(x)), and this is now returning the correct result

My next task is to select the first value from column 1 of the autofiltered list. I thought the following should do it

Code:
strStaffUsed = Sheets("staff available").AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells(2, 1).Value

However, this is returning the first value in the unfiltered list, which is currently hidden. Surely this should not happen, because it is not in the visible range?!

Question then, is how to return the first value from an autofiltered list?
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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