Results 1 to 6 of 6

Thread: Resolved - How to count the number of rows except hidden rows?

  1. #1

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Resolved Resolved - How to count the number of rows except hidden rows?

    I have some sort of data on my excel sheet in 30 rows. Let say A1 to A30.

    Here some rows were hidden. Let say A5 to A8 totally 4 rows were hidden. I just want to count the number of rows except hidden rows.

    I just tried the following code.

    VB Code:
    1. MsgBox ActiveSheet.UsedRange.Rows.Count

    This code counts the all row which are used in active sheet. The above code gives the answer 30. but my answer should be 26 not 30.

    Thanks in advance!

    CS.
    Last edited by cssriraman; Sep 4th, 2005 at 09:12 PM.

  2. #2
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: How to count the number of rows except hidden rows?

    How about a function to get it:
    VB Code:
    1. Option Explicit
    2.  
    3. Public Function COUNTVISIBLE(rng As Range) As Integer
    4. '   Counts visible cells
    5.     Application.Volatile True
    6.     Dim rcount%
    7.     Dim r As Range
    8.  
    9.     rcount = 0
    10.    
    11.     Set rng = Intersect(rng.Parent.UsedRange, rng)
    12.    
    13.     For Each r In rng
    14.         If Not r.EntireRow.Hidden And _
    15.         Not r.EntireColumn.Hidden Then _
    16.         rcount = rcount + 1
    17.     Next r
    18.    
    19.     COUNTVISIBLE = rcount
    20. End Function
    Justin Labenne
    www.jlxl.net

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to count the number of rows except hidden rows?

    Thats nice Justin, but how about all in one line of code?
    VB Code:
    1. Option Explicit
    2.  
    3. Public Function CountMe()
    4.     CountMe = ActiveSheet.Range("A1:A30").Cells.SpecialCells(xlCellTypeVisible).Count '=26
    5. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Resolved RESOLVED - Re: How to count the number of rows except hidden rows?

    Rob, That's great! Thanks a lot!

    Quote Originally Posted by RobDog888
    Thats nice Justin, but how about all in one line of code?
    VB Code:
    1. Option Explicit
    2.  
    3. Public Function CountMe()
    4.     CountMe = ActiveSheet.Range("A1:A30").Cells.SpecialCells(xlCellTypeVisible).Count '=26
    5. End Function
    Once again Thanks,

    CS.

  5. #5
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: Resolved - How to count the number of rows except hidden rows?

    As long as the user doesn't have all rows from 1-30 hidden, (serious longshot) and doesn't use it like a formula in a cell, looks very nice.
    Justin Labenne
    www.jlxl.net

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Resolved - How to count the number of rows except hidden rows?

    Correct.
    Let say A1 to A30.
    Thanks, but its the best there is for the range but if there is a need for an entire row then Justin has it hands down.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width