Originally Posted by
NoRomancelnChin
I think this will be easier to understand if I just set up a scenario for you from scratch. Open a blank sheet. In cells B2:D10 enter random digits between 1 and 10. In B1 type in =SUBTOTAL(9,B2:B10) which should give you the sum of that column. Use autocomplete to fill in columns C and D with the corresponding formula.
I want a function to go in column A that will take the average of each row WHICH takes into account hidden columns. When I type in =SUBTOTAL(101,B1:D1) in cell A1, it returns a divide by 0 error. I understand it's because I'm taking a subtotal of a subtotal and that can't work. The formula works for the cells underneath, because rows 2 to 10 are filled with raw data. However, it doens't take into account hidden columns. So far, I've written a macro that finds the average and takes into account hidden rows. This is the code I've written, and I assigned it to a shortcut key.........................
Sub AverageVisible()
Dim Rng As Range
Dim c As Range
MyRow = ActiveCell.Row
Set Rng = Range("H" & MyRow & ":CO" & MyRow).SpecialCells(xlCellTypeVisible)
For Each c In Rng
MySum = MySum + c.Value
Next c
For Each c In Rng
MyCount = MyCount + 1
Next c
Range("G" & MyRow).Value = MySum / MyCount
End Sub
......................
Is there a way to turn this into a user function such that I can type in the function name and a reference and it will find the average of the reference taking into account hidden columns? I don't know how to do this. Any help would be greatly appreciated. Thanks.
I have only converted the sub to function. You will have to copy this to standard module of the workbook and it will be available only to this workbook.
This function is "hardwired". In the sense it will always find out sum of cols H to CO in Active Cell row. Better way may be to have an option to give range as input to the function for it to claculate the average.
A V Veerkar
Bookmarks