Hi,
I’m used to editing macros in Excel VBA. So, say I want to say a value, x, in my macro should be taken from the value written in cell A2 in the spreadsheet, I’d write:
x = Range(“A2”)
My question is, what is the equivalent command to write in Basic code within a libreOffice Calc macro (i.e. x = ???)?
Kind Regards,
Brian
This action is recorded here a little longer
First get all sheets of this document
oSheets = ThisComponent.getSheets()
Now get one of them
oSheet = oSheets.getByIndex(0) ' First sheet by index (from 0 to .count()-1) '
or
oSheet = oSheets.getByName("Sheet1") ' By name '
Then get cell as object
oCellByPosition = oSheet.getCellByPosition(0, 1)
or
oCellRangeByName = oSheet.getCellRangeByName("A2")
And get value or string
nValue = oCellRangeByName.getValue()
sString = oCellRangeByName.getString()
Of course, you can write it all in one line:
sString = ThisComponent.getSheets().getByName("Sheet1").getCellRangeByName("A2").getString()
But I strongly discourage doing this. For example, if someday someone renames Sheet1, it will be much more difficult to find an error in a non-working macro.
To work with values of an entire range, rather than a single cell, it is better to use the .getDataArray()
method
That’s working now, thank you so much for your help!