[Solved] Problem calling a function
Posted: Thu Apr 20, 2017 2:44 pm
Hi all, I have written a function to compute the average of a range of cells in a column. My inputs are firstRow and lastRow. I need this function to be able to do the average of any cells its pointed to not just one specific range. So far im only able to get the function to work if i actually assign an integer value to firstRow and lastRow, ie 3 and 5 respectively which nets me the correct average from the spreadsheet i am calling it in. But i need this function to work with out actually assigning specific integers to it, i know i must assign something but i am just not sure what to do to get it to work to calculate multiple different ranges that it is called to do. As it stands it will only produce the average of cell 3 to 5( i just did this to test the actual math of it and help with debugging) . I just want any pointers or pushes in the right direction, any help of this sort would be greatly appreciated as I am struggling with it. I tired attaching a jpeg file but it would not let me for some reason
Thanks alot!!
Thanks alot!!
Code: Select all
Function Average(first As Long, _
last As Long) As Double
Dim avg as Double
Dim sum as Double
Dim row as Double
Dim first As Long
Dim last As Long
Dim DATCOL As Long
DATCOL = 1
sum = 0
first=3
last=5
For row = first To last
sum = sum + Activesheet.cells(row,DATCOL)
Next row
avg = sum / (last + 1 - first)
Average = avg
End Function