Cells(r,c) reference in macro

I am totally new to LibreOffice macro’s and wanted to fill or read a cell from my active spreadsheet.
I hoped that:

Cells(1,1) = 1

would assign the value 1 to $A$1

However I get
BASIC runtime error.
Sub-procedure or function procedure not defined.

Please help me for this first step!

How many sheets in your spreadsheet? And in which one do you want to change the value in A1?

Hello,

one (!) solution:

Sub SetCellValue()

   dim oDoc as object
   dim oSheet as object
   dim oCell  as object
   
   oDoc   = ThisComponent.CurrentController
   oSheet = oDoc.ActiveSheet
   oCell = oSheet.getCellByPosition(0,0)
   oCell.value = 1
   
End Sub 

Note(s)

  • indexing starts using 0, hence A1 equals (0,0)
  • If you want to read, use: var = oCell.value (numeric), var = oCell.string (string), or var = oCell.formula (formula) and oCell.Type to get the content type.

You may want to use the BASIC Reference Cards

Macro tested using:

Version: 7.0.4.2, Build ID: dcf040e67528d9187c66b2379df5ea4407429775
CPU threads: 8; OS: Linux 5.3; UI render: GL; VCL: kf5
Locale: de-DE (de_DE.UTF-8); UI: de-DE, Calc: threaded

Hope that helps

Yes this works

If the answer works for you, please consider to click the check mark (:heavy_check_mark:) next to the answer. Thanks in advance …