Issue with SpecialCells(xlCellTypeVisible)

Gringoire

Board Regular
Joined
Nov 18, 2016
Messages
71
Office Version
  1. 365
Platform
  1. Windows
Hello guys,
I'm using the below code on a Table containing about 25.000 rows

After filtering, about 5000 rows are still visible (I checked directly on my table) but even if the range rtotals still counts 25000 rows, the range rvisible only counts one or few rows (depending from filter arguments).
the size of rvisible seems to be stopped at the first row hidden by the filter.
it seems a strange behavior to me, because "SpecialCells(xlCellTypeVisible)" should skip the hidden rows, not stop at them...

any hint?


VBA Code:
Function voceDiStima()

    '*** FILTERING ROWS
    With shRawData.ListObjects("TabRawData").DataBodyRange                           
        .AutoFilter                                                                   
        .AutoFilter Field:=6, Criteria1:="C06065", Operator:=xlFilterValues        
        .AutoFilter Field:=34, Criteria1:="*-II0*", Operator:=xlFilterValues
    End With

Dim rtotal as Long,  rvisible as Long
rtotal= shRawData.ListObjects("TabRawData").DataBodyRange.Columns(8).Rows.Count
rvisible=shRawData.ListObjects("TabRawData").DataBodyRange.Columns(8).SpecialCells(xlCellTypeVisible).Rows.Count
End Function
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try using .Cells instead of .Rows
VBA Code:
rvisible=shRawData.ListObjects("TabRawData").DataBodyRange.Columns(8).SpecialCells(xlCellTypeVisible).Cells.Count
 
Upvote 0
Thanks for your answer, but unfortunately it raise an error 438 - Object doesn't support Property or Method.
Basing on google answers it seems that SpecialCells(xlCellTypeVisible) does not operate on filtered (non-contiguous) ranges :(

 
Upvote 0
It works for me:
Book1
ABCD
1PrestonJoeMaze
3xDustinZaiden
5xCoryLeonardo
6xTateIker
8
Sheet1



VBA Code:
Sub try()

Debug.Print ActiveSheet.ListObjects("Table1").DataBodyRange.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count 'result = 3, correct result
Debug.Print ActiveSheet.ListObjects("Table1").DataBodyRange.Columns(1).SpecialCells(xlCellTypeVisible).Rows.Count  'result = 1,  incorrect result

End Sub
 
Upvote 0
Hi @Gringoire , Akuini's solution works for me, but I've already seen it in other answers, that counting visible cells doesn't work for everyone, maybe it's a version problem.

I propose the following approach:

VBA Code:
Function voceDiStima()
  Dim obj As ListObject
  Dim i As Long, rvisible As Long, rtotal As Long

  Set obj = shRawData.ListObjects("TabRawData")
  
  With obj.DataBodyRange
    .AutoFilter
    .AutoFilter Field:=6, Criteria1:="C06065", Operator:=xlFilterValues
    .AutoFilter Field:=34, Criteria1:="*-II0*", Operator:=xlFilterValues
  
    For i = 1 To .Rows.Count
      If .Cells(i, 1).EntireRow.Hidden = False Then
        rvisible = rvisible + 1
      End If
    Next
    
    rtotal = .Columns(8).Rows.Count
  
  End With
  MsgBox "Total " & rtotal & " visible " & rvisible
End Function

Note: With 40,000 records, the response is immediate.

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 1
Solution

Forum statistics

Threads
1,215,079
Messages
6,122,998
Members
449,092
Latest member
masterms

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