how to count visible rows or columns?

jmckeone

Well-known Member
Joined
Jun 3, 2006
Messages
550
Ran across another post on the board concerning code to count non-hidden rows. However it would not work as a normal Sub but was written as a function. I am new to VBA and haven't a clue how to run a function nor what the benefit is of using a FUNCTION over a Sub. Can anyone help with this code as well as explain this question?

Code:
Function COUNTVISIBLE(Rng)

' Counts visible cells
    Dim CellCount As Long
    Dim cell As Range
    Application.Volatile
    CellCount = 0
    Set Rng = Intersect(Rng.Parent.UsedRange, Rng)
    For Each cell In Rng
        If Not IsEmpty(cell) Then
            If Not cell.EntireRow.Hidden And _
                Not cell.EntireColumn.Hidden Then _
                CellCount = CellCount + 1
            End If
    Next cell
    COUNTVISIBLE = CellCount
End Function
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
A quick description of the difference between SUB and FUNCTION:

A Sub is the standard routine that you create for manipulating Excel. You can move around, create and delete sheets and workbooks, and do all the stuff you are used to doing. When you use the recorder, the result is a Sub.

A Function, on the other hand, will return a calculated result to one cell. Functions can't manipulate objects at all. You also store Functions in standard modules (like Subs), but you must create Functions entirely in code. To use this function, say you have a range from A1:A205. Some of these cells are hidden, and you want to count the visible cells.
In A206 you could enter =COUNTVISIBLE(A1:A205) to get the result.

Hope this helps

Denis
 
Upvote 0
ok ... basically it is adding a custom formula type that can be accessed from the fx formula bar. Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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