Counting and suming number of visible rows

abberyfarm

Well-known Member
Joined
Aug 14, 2011
Messages
733
Hello,

The code below counts the number of rows, my question is, how would you count the number of visible rows?

Code:
Totalrows = ch1.Range("D" & Rows.count).End(xlUp).Row

also

How would you calculate the sum of the visible rows in column D, from D2 downwards?

I'm trying to write a formula,

Code:
Prob = "Subtotal sum of column D" / The number of visible cells"

Thanks for the help

john
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
In answer to your direct question, this will give you the number of visible cells in column D (including cells that are blank)
Code:
visCells = Range("D:D").SpecialCells(xlCellTypeVisible).Count
You can use the .End(xlUp) to limit your range to just the usedcells in the column.

In answer to your underlying question which seems to be how to get the average of the visible cells in column D, you could always use the oft forgotten SUBTOTAL formula with the 'exclude hidden' version of the Average function (101)

Code:
Prob = WorksheetFunction.Subtotal(101, Range("D:D"))
 
Upvote 0
Hi BiodideJ,

Thanks for the reply.

I tried this code, but it always returns Totalrows = 1, which is incorrect.

Code:
Totalrows = ch1.Range("D:D").SpecialCells(xlCellTypeVisible).End(xlUp).count

Is this the correct syntax for the subtotal function? It doesn't seem to summing the number correctly.

Code:
 Prob = WorksheetFunction.Subtotal(101, ch1.Range("D:D"))

For example, here totalrows should equal 9 and the subtotal should be 0.

Excel Workbook
ABCD
1SOCParking TimeDistanceDecision
119535300
289540320
899030300
1119520360
1309545320
1469530320
208955340
2099020300
2309515300
Charging
 
Upvote 0
Assuming data in Sheet1 beginning in row 2 (headers in row 1), maybe...

Code:
Sub aTest()
    Dim ch1 As Worksheet, Totalrows As Long, Prob As Long, dataRange As Range
    
    Set ch1 = Sheets("Sheet1")
    
    With ch1
        Set dataRange = .Range("D2:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
        Totalrows = dataRange.SpecialCells(xlCellTypeVisible).Count
        MsgBox Totalrows
        Prob = WorksheetFunction.Subtotal(101, dataRange)
        MsgBox Prob
    End With
    
End Sub

M.
 
Upvote 0
Here is a sample test code given your examples.

They both appear to do exactly what you are wanting.

Code:
Sub test()
Set ch1 = Sheets("Sheet1")
Prob = WorksheetFunction.Subtotal(101, ch1.Range("D:D"))
visCells = ch1.Range("D2:D" & ch1.Range("D" & ch1.Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible).Count
End Sub
The reason your visCells count was one was because you were getting the list of visible rows and then doing .END(xlUP) which would take you to a single row and then getting a count (of that single row). That was why the answer was always 1. I didn't really explain that part well so that was my fault.


EDIT: Marcelo's example is good as well, although if you use his, you will need to change the data type of Prob from Long to Double as it is an average.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,881
Messages
6,122,074
Members
449,064
Latest member
MattDRT

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