UDF to calculate average

froswick

New Member
Joined
Jan 6, 2009
Messages
2
Hi, my first post and very new to VBA so any help appreciated. I'm trying to create a function to calculate the average of a range of cells in a column. The first cell is always the cell to the left of the cell where the UDF will be used, the last cell in the range will depend on a parameter passed to the function ie if 3 is passed, the range will be three cells deep etc
I thought this would work but no joy :-(

Function wkavg(hours As Single)
ActiveCell.FormulaR1C1 = "=SUM(RC[-1}:R[" & hours & "]C[-1])"
End Function



Any ideas please? Thanks.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You don't need a UDF.

Excel Workbook
AB
1100200
2200*
3300*
4400*
5500*
Sheet1


3 is your factor

edit: if you really wanted a UDF

Code:
Function MyAverage(rngReference As Range, factor As Integer) As Variant
MyAverage = Evaluate("AVERAGE(OFFSET(" & rngReference.Address & ",,," & factor & "))")
End Function
 
Last edited:
Upvote 0
Thanks very much, that's just what I need, you're right, I don't need a UDF it seems. Thanks for your help, much appreciated..
 
Upvote 0
Thanks for the tips in this thread. I used what I read here to create a PPM calculator.
Code:
Function PPM(experimental As Range, Theoretical As Range) As Variant
Dim countfactor As Integer
Dim avgExp As Double
Dim avgThe As Double
countfactor = experimental.Count
avgExp = Evaluate("AVERAGE(OFFSET(" & experimental.Address & ",,," & countfactor & "))")
avgThe = Evaluate("AVERAGE(OFFSET(" & Theoretical.Address & ",,," & countfactor & "))")
PPM = 1000000 * ((avgExp - avgThe) / avgThe)
End Function
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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