Search code examples
vbaexcelvisibleinvisible

Consistant way to check if entire range is hidden


I have an extensive code for adjusting filters and reading the unique values into comboboxes etc. and it's working almost perfectly, however this one line of code is sometimes not identified correctly (only when I filter for certain values though it seems). It is supposed to prevent errors in case all the rows are hidden due to the filters that have been applied through the makro.

If RangeCombo.EntireRow.Hidden = False Then

RangeCombo contains both hidden and unhidden rows when it works and when it doesn't, so I really don't know what could be causing this. I tried this:

If RangeCombo.SpecialCells(xlCellTypeVisible).Count > 0 Then

but it throws up an error if there are no visible cells in the range.

I also tried:

    Set x = RangeCombo.Find("*", , xlFormulas, xlWhole)
    If Not x Is Nothing Then

but that will find cells, even if the entire range is hidden...

I'd be happy for any solutions that might help me circumnavigate the xlCellTypeVisible error (when all cells in RangeCombo are empty) or any other way I might achieve my goal.


Solution

  • While checking the autofiltered cells, always include the header row in the range and check if the visible cells count is greater than 1 like below...

    If Range("A1:A" & LastRow).SpecialCells(xlCellTypeVisible).Count > 1 Then
    

    Where LastRow is the last row with data on the sheet. Change the range as per your requirement.

    If no row is returned after applying autofilter, the count would be equal to 1 as only header row would be visible and you can skip doing some actions on visible rows/cells.