Consulting

Results 1 to 4 of 4

Thread: Count non empty cells on sheet with hidden cols

  1. #1

    Count non empty cells on sheet with hidden cols

    Hi,

    Im trying to count the number of non empty cells on a worksheet but there are hidden columns on the sheet. The code im using counts them too how can i count only the "non hidden , non empty cells "

    using the following code at the moment:

     
    Nocells = WorksheetFunction.CountA(Sheets("Results").UsedRange)

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    There is probably a better way but you could use a loop to go through columns and check if they are hidden then count.

    [VBA]Sub CountNonHidden()
    Dim c As Range
    Dim x As Integer

    For Each c In Sheet1.Columns

    If c.EntireColumn.Hidden = False Then
    x = x + WorksheetFunction.CountA(c)
    End If

    Next

    MsgBox x

    End Sub[/VBA]

    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I think that you would have to ikterate all the columns, find the hidden ones,
    count the cells in those, and subtract them.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Mentor MaximS's Avatar
    Joined
    Sep 2008
    Location
    Stoke-On-Trent
    Posts
    360
    Location
    try to use that:

    [vba]
    Sub Count_Visible()

    Dim LCol, i, j As Long

    'This will check for last used column
    LCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column

    j = 0

    For i = 1 To LCol
    If Columns(i).Hidden = False Then
    j = j + WorksheetFunction.CountA(Columns(i))
    End If
    Next i

    End Sub
    [/vba]

Posting Permissions

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