Link to home
Start Free TrialLog in
Avatar of derekackerman
derekackermanFlag for United States of America

asked on

Harmonic Mean VBA Macro Excel

I need to comput the H.M. of a column of numbers, this doesn't work:

ActiveCell.Formula = "=rX/SUM(1/B2:B" & lrow & ")"

for each cell in row 2 to the last row, I need to sum the Reciprocals (1/cell value), and then divide that into the number of cells (N)

Or is there a built in function for it?
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Hello derekackerman,

If you are using the Analysis ToolPak, you will have the HARMMEAN() function, that will do this rather easily.

Regards,

Patrick
Sorry, the function name is HARMEAN()
BTW, you can also calculate harmonic means with an array formula.  For example, this finds the harmonic
mean for A1:A5...

{=1/(SUM(1/A1:A5)/COUNT(A1:A5))}

To enter an array formula, do not enter the curly braces, and hit Ctrl+Shift+Enter instead of Enter to finish it off.  
Excel will then display those braces to indicate that it's an array formula.
derekackerman,

So, in VBA, you can do this to get your array formula:

ActiveCell.FormulaArray = "=1/(SUM(1/B2:B" & lrow & ")/COUNT(B2:B" & lrow & "))"

Patrick
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of derekackerman

ASKER

I like the last answer the best!