## [Calc] Function to compute average of a range

Creating a macro - Writing a Script - Using the API

### [Calc] Function to compute average of a range

Can somebody help me ?
The following is the question:

Function ComputeAverage(firstRow As Long, lastRow As Long) As Double
The aim of this function is to calculate the average of a section of data in Column A
For example, a call of ComputeAverage(4,10) in your OO Basic program should return the average of the cells from A4 to A10
You can make use of the constant DATA_COL (=1) in this function to refer to Column A
The first input firstRow is the row number of the starting row
The second input lastRow is the row number of the last row

Title Edited. A descriptive title for posts helps others who are searching for solutions and increases the chances of a reply (Hagar, Moderator).
Last edited by axljerry123 on Tue Apr 18, 2017 3:12 pm, edited 1 time in total.
OpenOffice 4.1.3
axljerry123

Posts: 2
Joined: Tue Apr 18, 2017 2:16 pm

### Re: Can somebody help me ? :( :(

On Forum policy is not to do students' homework for them, so any answers you receive will be predicated on that.

Start by telling us how you would average a list of data points using pen and paper, and someone may guide you how approach this using a spreadsheet.
Apache OpenOffice 4.1.5 on Xubuntu 18.04 (mostly 64 bit version) and infrequently on Win2K/XP
14 October 2016 was Pooh's 90th birthday

RoryOF
Moderator

Posts: 27184
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

### Re: Can somebody help me ? :( :(

Do you know anything about the API functions? (You must use them to control the calc doc/sheet/cells).

You must get the active Calc document (Thiscomponent), and then you must get the desired Sheet (by number, by name, or the actually displayed (Current selection...) Then you can get the cells by coordinates and the numeric values of the cells. Summarize them, and divide the result by the number of the rows (/lastrow-firstrow)

And what about the empty cells? They will represent a zero value, and it increases the divider. Do you need use them in the average value?
Tibor Kovacs, Hungary; LO4.4.7, LO5.4.7 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.0 and AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.

Zizi64
Volunteer

Posts: 7165
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary