Page 1 of 1

[Solved] Can't use MIN or MAX in my macro function

Posted: Sat May 08, 2010 7:30 am
by sedwick1024
I have a OO BASIC function in which I'm trying to use MIN the way you would in a spreadsheet cell (swapping ; for , of course), and every time I enter a call to the function in a cell, it gives me a runtime error, saying "Sub-procedure or function procedure not defined", pointing me to the line with the MIN call. Do I have to add a line to import a library or something? Thanks and sorry if this has been answered elsewhere...

Re: Can't use MIN or MAX in my macro function

Posted: Sat May 08, 2010 2:39 pm
by ms777
Calc functions usable in calc formulas have nothing to do with OO Basic functions. AFAIK, there is no Min/max in OO Basic. You have to define it yourself

Re: Can't use MIN or MAX in my macro function

Posted: Sat May 08, 2010 3:11 pm
by FJCC
I think the MIN function can be used in a macro. Here are two examples.

Code: Select all

Sub Exmpl1
Dim Data(4)
Data(0) = 15
Data(1) = 53
Data(2) = 3
Data(3) = -43
Data(4) = 5
svc = createUnoService( "com.sun.star.sheet.FunctionAccess" )
MinValue = svc.callFunction("Min",Data())
Print MinValue
End Sub

Sub Exmpl2
Sheet = ThisComponent.Sheets.getByIndex(0)
oCellRange = Sheet.getCellRangeByName("A1:A10")
svc = createUnoService( "com.sun.star.sheet.FunctionAccess" )
arg = Array(oCellRange)
MinValue = svc.callFunction("Min",arg)
Print MinValue
End Sub

Re: Can't use MIN or MAX in my macro function

Posted: Sat May 08, 2010 5:41 pm
by FJCC
Expanding on my last post a bit, you can access any spreadsheet function with the com.sun.star.sheet.FunctionAccess service. You just have to pass it the function name and the arguments of the function in an array. You can use the Vlookup function like this.

Code: Select all

Sub Exmpl3
Sheet = ThisComponent.Sheets.getByIndex(0)
oCellRange = Sheet.getCellRangeByName("A1:B10")
SearchValue = Sheet.getCellRangeByName("C1").getString()
Column = 2
Mode = 0
svc = createUnoService( "com.sun.star.sheet.FunctionAccess" )
arg = Array(SearchValue, oCellRange, Column, Mode)

Value = svc.callFunction("VLOOKUP",arg)
Print Value
End Sub
You can even use an array function, such as the FREQUENCY function. Naturally, you get an array back in that case.

Code: Select all

FreqArray = array(oCellRange, oCellRange2)
oCellRange3.setData(svc.callFunction( "FREQUENCY", FreqArray ))

Re: Can't use MIN or MAX in my macro function

Posted: Sun May 09, 2010 4:05 am
by sedwick1024
That seems to work, thanks very much! Now I just need to figure out how to not get an error message for EVERY instance of a syntax error in my function's evaluation...

Re: Can't use MIN or MAX in my macro function

Posted: Tue Mar 08, 2016 3:34 am
by RichieRH
FJCC wrote:I think the MIN function can be used in a macro. Here are two examples.

Code: Select all

Sub Exmpl2
Sheet = ThisComponent.Sheets.getByIndex(0)
oCellRange = Sheet.getCellRangeByName("A1:A10")
svc = createUnoService( "com.sun.star.sheet.FunctionAccess" )
arg = Array(oCellRange)
MinValue = svc.callFunction("Min",arg)
Print MinValue
End Sub

I can't use this code in my data calc. I have some data in sheet (0) in the range cell (A1:A1000) but when I run this macro, the results of 'print minvalue' didn't show anything..Do you have an example that works??I mean in ods file...

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Tue Mar 08, 2016 4:13 am
by FJCC
Here is the original file.

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Tue Mar 08, 2016 5:05 am
by RichieRH
FJCC wrote:Here is the original file.
I have tried your code and it s working like a charm..but why is not in my file??

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Tue Mar 08, 2016 6:44 am
by FJCC
You really had me confused for a while.The problem is that the name of the procedure is the same as the name of the variable you are trying to print. Both are called MaxValue. Try

Code: Select all

Sub MaxValue
Sheet = ThisComponent.Sheets.getByIndex(0)
oCellRange = Sheet.getCellRangeByName("A16:A40")
svc = createUnoService( "com.sun.star.sheet.FunctionAccess" )
arg = Array(oCellRange)
MaximumValue = svc.callFunction("Max",arg)
Print MaximumValue
End Sub

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Tue Mar 08, 2016 7:40 am
by RichieRH
Yes, it's now working.Thanks a lot...FJCC
Indeed..It's an easy simple things to solved but sometimes we often think too far and forget the basic case.



Sorry, English lang is not my native.

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Thu Oct 20, 2016 4:54 pm
by malazkd
Hi! I have a problem in macro scripting. Is WORKDAY function available? I don't know exactly how to write it on vb.

Code: Select all

REM  *****  BASIC  *****

Sub Main

Dim svc As Object
Dim result
Dim oDate As Date
Dim oValueAdd
Dim oHolidayRange

Doc 				= ThisComponent 'refers to the whole file as object
  
 Request 				= Doc.Sheets(0)
 HolidayList 				= Doc.Sheets(1)
   oDataRange = Request.getCellRangeByName("E1:E1000")
   oHolidayRange = HolidayList.getCellRangeByName("A1:A1000")
   oDate =  Request.getCellRangeByName("B3").Value()
   oValueAdd =  Request.getCellRangeByName("B2")
   'test = array(oDate,oValueAdd,oHolidayRange)
   
   svc = createUnoService("com.sun.star.sheet.FunctionAccess")
   result = svc.callFunction("WORKDAY",Array(oDate,oValueAdd,oHolidayRange))
   'result = workday(test)
   MsgBox(result)

End Sub

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Thu Oct 20, 2016 6:10 pm
by Villeroy
What is the meaning of all this? Why can't you use a simple spreadsheet? The purpose of spreadsheet software is that non-programmers do not have to write any programs to calculate weekdays, minimums, maximums etc.
Obviously you are not a programmer.

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Thu Oct 20, 2016 6:18 pm
by malazkd
Hello Sir Villeroy, sorry but I am a beginner dev and I'm new in macro scripting.

I am developing a template that generates multiple requests to be uploaded in a system.

I need WORKDAY to compute due date for each request. Would you know how to implement WORKDAY in macro?.... Thanks.

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Thu Oct 20, 2016 6:31 pm
by RoryOF
Have you used the Forum search (top right of forum window) to search for Workday? There are a number of threads on the subject.

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Thu Oct 20, 2016 7:42 pm
by FJCC
Here is an example of using WORKDAY() in a macro.

Code: Select all

oSheet = ThisComponent.Sheets.getByName("Sheet3")
StartDate = oSheet.getCellrangeByName("A1").Value
DaysToAdd = oSheet.getCellrangeByName("B1").Value
oHolidays = oSheet.getCellrangeByName("C1:C2")
svc = createUnoService("com.sun.star.sheet.FunctionAccess")
result = svc.callFunction("WORKDAY",Array(StartDate,DaysToAdd,oHolidays))
print result

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Thu Oct 20, 2016 9:17 pm
by Villeroy
malazkd wrote:Hello Sir Villeroy, sorry but I am a beginner dev and I'm new in macro scripting.
I am developing a template that generates multiple requests to be uploaded in a system.
I need WORKDAY to compute due date for each request. Would you know how to implement WORKDAY in macro?.... Thanks.
No, I only know how to implement such things without macros.

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Fri Oct 21, 2016 7:26 am
by malazkd
Thanks FJCC! I discovered that I am already implementing correct set of codes/syntax (same as yours)
I was encountering issues because my range in Holidays includes the column name/header.

it should be "E2:E30" and not "E1:E30"

Thank you so much everyone!

Re: [Solved] Can't use MIN or MAX in my macro function

Posted: Fri Oct 21, 2016 12:34 pm
by Villeroy
This is the kind of problem which can be cleared up easily by entering the equivalent formula into a spreadsheet cell. You did not even tell us why you think that something is wrong with your code (in fact there is nothing wrong with it). No error message, no actual sheet data, no debugging result, nothing.