Page 1 of 1

[Solved] Problem calling a function

Posted: Thu Apr 20, 2017 2:44 pm
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

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Posted: Thu Apr 20, 2017 3:34 pm
by Villeroy
=AVERAGE(OFFSET($A$1;first-1;0;last-first;1))

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Posted: Thu Apr 20, 2017 5:31 pm
by FJCC
Don't declare first and last with DIM after you have declared them AS Long in the first line of the function.

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Posted: Thu Apr 20, 2017 5:52 pm
by RusselB

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Posted: Thu Apr 20, 2017 11:01 pm
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.

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Posted: Thu Apr 20, 2017 11:14 pm
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?

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP(SOLVED)

Posted: Thu Apr 20, 2017 11:18 pm
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?

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Posted: Thu Apr 20, 2017 11:25 pm
by Villeroy
Too sad. Just another cargo cult programmer.

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Posted: Fri Apr 21, 2017 12:33 am
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?

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Posted: Fri Apr 21, 2017 12:33 am
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.

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP [SOLVED]

Posted: Fri Apr 21, 2017 12:39 am
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 !

Re: PROBLEM WITH FUNCTION CALLING PLEASE HELP

Posted: Fri Apr 21, 2017 12:42 am
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