Intro
This is a follow-up to AVERAGE for unhidden columns...and SUM and COUNT
The code AVERAGE for unhidden columns...and SUM and COUNT provides a focused solution for someone who wants to be able to find the average of cells in a row, excluding those in hidden columns. In a sense, all she wanted was the Worksheet function to work horizontally as well as vertically. That's not unreasonable.
Building on success
The one-function-at-a-time focused solution is the basis for a function that can be used for rows instead of columns. All that was needed was a SELECT CASE ... END CASE.
- To be consistent, to uses the same inputs as the SUBTOTAL function, i.e. the Operation and the target ranges.
- The 1-11 operations values (1 for Average, etc) are omitted because I did not want to continue the confusion of the poorly described difference between the 1-11 range and the 1-111 range. Hidden is Hidden, and my understanding of the 101-111 range is more in line with the behavior I think I see. Either way, it would be simple to change back to the 1-11 range.
Function SubTotalColumns(Operation As Integer, ParamArray rngSource())Application.VolatileDim rngCell As RangeDim rngVisible As RangeDim dblCtr As Double
For dblCtr = LBound(rngSource) To UBound(rngSource)For Each rngCell In rngSource(dblCtr).Cells
If Not rngCell.Columns(1).Hidden ThenIf rngVisible Is Nothing ThenSet rngVisible = rngCellElseSet rngVisible = Union(rngVisible, rngCell)End IfEnd IfNext rngCellNext dblCtrSelect Case OperationCase 101SubTotalColumns = Application.WorksheetFunction.Average(rngVisible)Case 102SubTotalColumns = Application.WorksheetFunction.count(rngVisible)Case 103SubTotalColumns = Application.WorksheetFunction.CountA(rngVisible)Case 104SubTotalColumns = Application.WorksheetFunction.max(rngVisible)Case 105SubTotalColumns = Application.WorksheetFunction.min(rngVisible)Case 106SubTotalColumns = Application.WorksheetFunction.Product(rngVisible)Case 107SubTotalColumns = Application.WorksheetFunction.StDev(rngVisible)Case 108SubTotalColumns = Application.WorksheetFunction.StDevP(rngVisible)Case 109SubTotalColumns = Application.WorksheetFunction.Sum(rngVisible)Case 110SubTotalColumns = Application.WorksheetFunction.var(rngVisible)Case 111SubTotalColumns = Application.WorksheetFunction.varP(rngVisible)End SelectEnd Function
No comments:
Post a Comment