Consistant way to check if entire range is hidden

心已入冬 提交于 2021-01-08 06:28:07

问题


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.


回答1:


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.




回答2:


Ignoring errors is not generally advisable but in certain circumstances it is legitimate. So, you could do this

Sub x()

Dim r As Range

On Error Resume Next
Set r = RangeCombo.SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If Not r Is Nothing Then
    MsgBox "Visible"
Else
    MsgBox "All hidden"
End If

End Sub


来源:https://stackoverflow.com/questions/44052817/consistant-way-to-check-if-entire-range-is-hidden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!