Question

I am hiding the rows by using following lines of code excluding top two rows only because they are Headers.

For i=3 To ThisWorkBook.Sheets("ALL").Range("A1",ThisWorkBook.Sheets("ALL").Range("A65536").End(xlUp)).Rows.Count
    ThisWorkBook.Sheets("ALL").Rows(i).EntireRow.Hidden=True
Next

Now to get the hidden rows count i am using following lines of code

From i=3 To ThisWorkBook.Sheets("ALL").Range("A1",ThisWorkBook.Sheets("ALL").Range("A65536").End(xlUp)).Rows.Count
    ThisWorkbook.Sheets("ALL").EntireRow.Hidden=False
Next

But I am getting rowcount as 2. Infact the sheet has 10 rows. So how to make hidden rows as visible?

Was it helpful?

Solution

If I understand your question correctly, here is an alternative way to approach your problem that will give you some code that runs faster and is a pretty clean to manage:

Option Explicit

Sub CountHiddenRows()

Dim wks As Worksheet
Set wks = ThisWorkbook.Sheets("ALL")

With wks

    Dim lngLastRow As Long
    lngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row

    .Range("A3:A" * lngLastRow).EntireRow.Hidden = True

    Dim rngConsider As Range
    Dim lngHiddenRows As Long, lngRows As Long, lngVisibleRows As Long

    Set rngConsider = .Range("A1:A" & lngLastRow)

    lngRows = rngConsider.Rows.Count
    lngVisibleRows = rngConsider.SpecialCells(xlCellTypeVisible).Rows.Count
    lngHiddenRows = lngRows - lngVisibleRows

    MsgBox "There are " & lngHiddenRows & " hidden rows."

End With

End Sub

OTHER TIPS

Worksheet.Rows.Count always returns the total number of rows in a sheet.
Here I would use Worksheet.UsedRange.Rows.Count.
Also, your code risks to fail when the first cell of the last row is empty.
Here is a little function that counts the number of visible rows between row 2 and the last used row.

Function CountVisibleRows() As Integer
  Dim R As Integer
  For R = 2 To UsedRange.Rows.Count
    If Not Rows(R).Hidden Then CountVisibleRows = CountVisibleRows + 1
  Next R
End Function

I usually use a cycle when the number of rows is small, because I have better control and it's often faster.
When I work with thousands of rows then it's faster asking Excel to use the SpecialCells or other functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top