[Solved] Problem calling a function

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
mrgixxer88
Posts: 5
Joined: Thu Apr 20, 2017 1:59 pm

[Solved] Problem calling a function

Post by mrgixxer88 »

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!!

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
Last edited by mrgixxer88 on Fri Apr 21, 2017 12:35 am, edited 2 times in total.
open office 4.1.3 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Post by Villeroy »

=AVERAGE(OFFSET($A$1;first-1;0;last-first;1))
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Post by FJCC »

Don't declare first and last with DIM after you have declared them AS Long in the first line of the function.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
RusselB
Moderator
Posts: 6646
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Post by RusselB »

OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
mrgixxer88
Posts: 5
Joined: Thu Apr 20, 2017 1:59 pm

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Post by mrgixxer88 »

does declaring first and last as Dim stop the function operating correctly and getting it to do what I want ? it is similar to that post, except I have got this far by myself and only would like some pointers or know how , not for anyone to do it for me haha. Villeroy I need to do it as a user made function run so I don't think I can use that code but thank you for your reply.
open office 4.1.3 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Post by Villeroy »

From scratch without office suite at hand:

Code: Select all

Function AVG(startRow,endRow) AS Double
Dim fa, sh, rg
  fa = createUnoService("com.sun.star.sheet.FunctionAccess")
  sh = ThisComponent.CurrentController.getActiveSheet()
  rg = sh.getCellRangeByPosition(0,startRow,0,endRow)
  AVG = fa.callFunction("AVERAGE", Array(rg))
End Function
But why this complicated bullshit when you have a spreadsheet application at hand?
Last edited by Villeroy on Thu Apr 20, 2017 11:25 pm, edited 1 time in total.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
mrgixxer88
Posts: 5
Joined: Thu Apr 20, 2017 1:59 pm

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP(SOLVED)

Post by mrgixxer88 »

I have never seen any of that type of code before Villeroy, I think it's a bit too advanced for me. Just got it too work following FJCC's advice of not declaring the variables again. does declaring them again confuse the function or something?
open office 4.1.3 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Post by Villeroy »

Too sad. Just another cargo cult programmer.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
mrgixxer88
Posts: 5
Joined: Thu Apr 20, 2017 1:59 pm

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Post by mrgixxer88 »

cargo cult programmer Vileroy? I'm not a programmer actually not have I ever claimed to be. All I did was ask for a little a bit of help and I received it. Is it it really necessary to insult me? I had to do the function that way as I was told to do the function that way, not to use the spreadsheet functions. Thank you for your replies, I'm sorry you find me a cargo cult programmer......i don't even know what that is to be honest?
open office 4.1.3 on Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Post by FJCC »

Declaring the variables again does not confuse the function, it results in the values that were passed to the function being erased. The way you had it, you declared the variables first and last as parameters passed to the function then in the body of the function you declared two variables also called first and last. When those are created they are given the value zero, overwriting the values that you passed to the function.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
mrgixxer88
Posts: 5
Joined: Thu Apr 20, 2017 1:59 pm

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP [SOLVED]

Post by mrgixxer88 »

ah I see, I noticed that when I used break points to try and debug it I was getting zero for everything when I hovered the cursor over the items, I couldn't understand why. So when doing a function like that you should only have the variables declared in the title of the function ? Thanks for your help mate, I do appreciate it !
open office 4.1.3 on Windows 10
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Post by JeJe »

FJCC - I think it creates a new variable, though that might be what you meant.

Code: Select all

    Function Average(first As Long,last As long) As Double
                           
                            Dim first As Long
                            Dim last As string
                         msgbox vartype(last) '=8 string
                            
                            end function

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply