Use cell formula in a macro

Hi I’m practicing Formulai and macros. I have a formula that works perfectly well in a cell but fails when used as a Macro. A syntax error is declared (parenthesis). if you could point out my mistake I would appreciate it Thanks
It’s really simple, I’m just comparing 2 cell values (is cell B1 > A1) =IF (OFFSET(C1,0,-1)>OFFSET(C1,0,-2),“yes”,“no”)
I call the function in D1 and get the error as shown in the image

example.ods

Thanks

Hello,

you can’t use formulas the way you tried to use `em. In fact spreadsheet functions do not exist in BASIC directly. You need to create an object which provides access to spreadsheet function similar to the following snippet:

...
Dim oFA  As Object 

oFA = CreateUNOService("com.sun.star.sheet.FunctionAccess") 
MyEval = oFA.callFunction("OFFSET",Array())
...

where Array() contains the parameters to the spreadsheet function in use.

Hence: Try to avoid using spreadsheet functions in BASIC macros, since in most cases it can be done using other methods more efficiently (see also: LibreOffice Help - CreateUnoService Function)

In your case I’d prefer (outline - does work, but no error handling):

Function TLWU( f1 as Double, f2 as Double ) as String

  If f2 > f1 then
    TLWU="yes"
  Else
    TLWU="no"
  End If
  
End Function

and call it using =TLWU(A1;B1)

Hi Thank you very much for your reply & the information, Very helpfull, you have given me something to work with.
Regards Dave