Basic pass a range to function to create

I can’t find how to pass a range to my “MyFirstValue” function.
I’ll give you a very simple example to explain.
In the cell:
“=MyFirstValue(A1:B20)”
In the function:
Function MyFirstValue(rng As Object) As string
oCell = rng.getCellByPosition(0, 0)
MyFirstValue = “MyFirstValue=” & oCell.Value
End Function

When I call the function I get this error message:
BASIC runtime error.
Object variable not set.

You can not pass a range as an Object to a custom function. You can pass the numerical and textual values of the range as a two dimensional array.

Passed range values as an Array.ods (21.5 KB)

Thank you so much truly.
ChatGPT only gave me wrong directions and led me astray.
I wasted two days looking for solutions online.
It’s actually the most logical thing but it didn’t come to mind.
Thanks so much again.

what prompt did you try ?

Apache OpenOffice Community Forum - [Solved] Passing a range to a user defined function - (View topic)

I can’t believe it… A programmer (even if he is a beginner) ask the ChatGPT: how to write LO macros…
Why?!?!?!

cause the “documentation” is messy, redundant, often outdated, not well indexed,
the API itself not so clear,
the IDE a pain (for Basic),
the execution not so stable
etc etc … :wink:

may be …?!
but Chatgpt was »trained« with exactly this data, in what way should this stochastic parrot provide better answers?

1 Like

maybe speed over Monte Carlo method - Wikipedia
or just Placebo - Wikipedia :innocent:

interesting prism to evaluate the answers in this very site either :tired_face:

… if you don’t use VBASupport 1.
Try this function:

Option VbaSupport 1

Function TestFunc(arg)
  Dim oRange, typeArg As String, v, val
  typeArg=TypeName(arg)
  If typearg="Range" Then
    Set v=arg
    Set oRange=v.CellRange  
    val=oRange.AbsoluteName
  ElseIf typearg="Variant()"  Then 
    val="(" & Lbound(arg, 1) & " to " & Ubound(arg, 1) & ", " & _
              Lbound(arg, 2) & " to " & Ubound(arg, 2) & ")" 
  Else
    val=arg
  End If
    
  TestFunc=typeArg & ": " & val
End Function

Then

=TESTFUNC(A1)
=TESTFUNC(A2:D7)
=TESTFUNC(12554)
=TESTFUNC("ABC")
=TESTFUNC({1;2;3})
2 Likes

… but be cautious. In a module using Option VBAsupport 1 some Basic commands work differently also in user code not explicitly relying on VBA support. (I don’t know an index of the cases.)

2 Likes

There is another non-obvious point.
If we pass a cell array containing formulas to a user-defined function as an argument in VBASupport 1 mode, then when analyzing the argument cells we may encounter “dirty” cell values.

IMHO …the most stupid case:

'try with|without »Option VBAsupport 1«
print (rgb(255,100,60))

There are historical reasons for this.