How can I use a cell range in a macro function?

I would like to pass a range in a function and use that in a macro but cannot figure out how to do it.

I tried to create a formula in a cell like:
=TEST(A2:A6)

and then a routine like:

Public Function Test(r as com.sun.star.table.CellRange) as String
cnt = 0
oEnum = r.createEnumeration()
Do while oEnum.hasMoreElements()
	cnt = cnt + 1
	oEnum.nextElement()
Loop
	Test = ">> " & cnt & " " & typename(r) & " " & isObject(r) & " " &  IsNull(r) & " " & ThisComponent.Sheets(0).getCellRangeByName("A2:A4").computeFunction(com.sun.star.sheet.GeneralFunction.COUNT)
End Function

but no matter what I try I get errors or strange behaviour. Anyone knows how to do this?

thx a lot.

May be so?

HI @startu – Still looking for an answer here?

It looks like @JohnSUN pointed at a earlier similar question that was answered: Count cells in a range in macro This is a duplicate unless otherwise decided later.