Why doesn't this work then eh?

Mike Blackman

Well-known Member
Joined
Jun 8, 2007
Messages
2,494
Hi All,

Anyone have any idea why this doesn't work and can anyone think of any efficient ways of counting the number of hidden rows without looping through the usedrange?

Code:
wks.Columns.Count - wks.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count

FYI... the problem is that when there is are hidden row(s) its returning rows.count - the last row before the first hidden row.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Eh, how are the rows being hidden?
 
Upvote 0
Change the wks.Columns.Count to wks.Rows.Count if you're interested in hidden rows:
Code:
    MsgBox ActiveSheet.Rows.Count - ActiveSheet.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count
 
Upvote 0
Code:
wks.Columns(1).Cells.Count - wks.Columns(1).Cells.SpecialCells(xlCellTypeVisible).Cells.Count
 
Upvote 0
Meh.... Thanks Richard... dropping .Cells seems to of worked a treat! Conceptually why does it matter about the .Cells though?

@Norie, I was testing with actual Hidden Rows although the method after losing .cells works with both which is conveniently what I'm after! :)

@Misca, appreciate the example posted doesn't make logical sense, the actual code is however properly constructed.
 
Upvote 0
I presume it's something to do with the SpecialCells method returning a range collection and a range collection not having a Cells property (so VBA defaults to the first area in the collection) giving you the wrong value. Collections do have count properties though so using Count not prefixed with Cells seems to work.

Summat like that anyhow ;-)

Probably if Mary (Rory) or The Beard (Colin) drop by they can provide a definitive answer.
 
Upvote 0
Richard,
What are you talking about? :)
Specialcells returns a Range object, not a collection of range objects, and does have a Cells property.
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,472
Members
452,915
Latest member
hannnahheileen

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