, 2 min read
Programming in Excel VBA Compared To LibreOffice (OpenOffice) Calc
Original post is here eklausmeier.goip.de/blog/2013/10-27-programming-in-excel-vba-compared-to-libreoffice-openoffice-calc.

Programming in Excel VBA is quite easy. Below are some simple basic concepts to access and change the spreadsheet data.
- Worksheets(i)references the i-th sheet, usually they are called Sheet1, Sheet2, Sheet3
- Cells(i,j)references i-th row, j-th column, e.g.,- Cells(2,3)is C2
- Cells(i,j).Valueis the content of the cell
- `Cells(i,j).AddComment("my comment")` adds a comment to the cell
- Cells(i,j).Interior.Colorsets the background color of the cell
- MsgBox(string)for displaying a short text in a message box
- IsEmpty(cell)checks whether cell is empty
- Worksheets(i).Cells.Interior.ColorIndex=xlNoneto clear background color in all cells in a worksheet
- Worksheets(i).Cells.ClearCommentsto clear all comments in all cells in a worksheet
- Worksheets(i).Cells.Clearto wipe out all content in a worksheet
- Worksheets(i).Activatemakes i-th worksheet active (bring to foreground)
- Application.Dialogs(xlDialogSaveAs).Show "filename.csv", xlCSVopen file-save-as dialog
- UCase(...)return upper case of argument
The usual BASIC-style statements are used, e.g.,
- for i=1 to n ... next
- if ... then ... else ... end if
- select case variable case value ...end select
- Dim x as String,- Dim i as Integer
That's basically all it needs to program in Excel VBA.

Doing the same in LibreOffice (OpenOffice) is somewhat similar but usually much more arcane and more dogmatic. Information on various BASIC functionality seems to be spread across various internet forums.
In case one wants a push-button in the spreadsheet: The connection between clicking on a push button and its association with a subroutine is not by naming convention but rather by explicit association with Events.
Now the above Excel functionality in LibreOffice.
- ThisComponent.getSheets().getByIndex(0)references the first sheet, usually called Sheet1
- ThisComponent.getSheets().getByIndex(0).getCellByPosition(i,j).getString()fetching character string from cell at row (i+1), and column (j+1)
- ...getValue()for the numeric value
- ThisComponent.getSheets().getByIndex(0).Annotations.insertNew( ThisComponent.getSheets().getByIndex(0).getCellByPosition(0,0).getCellAddress, "New comment" )for adding a comment
- ThisComponent.getSheets().getByIndex(0).getCellByPosition(0,0).CellBackcolor = 48000sets the background color
- MsgBox(string)as above
- ThisComponent.getSheets().getByIndex(0).getCellByPosition(0,0).Type = 2to check if cell is empty
- ThisComponent.getSheets().getByIndex(1).ClearContents( 1023 )to delete everything in that sheet
- See ClearContents()above
- See ClearContents()above
- ThisComponent.getcurrentController.setActiveSheet( ThisComponent.getSheets().getByIndex(2) )to activate sheet #3
- Somehow the lines with ThisComponent.storeAsUrl("file://tmp/Test.csv", args2() ), but didn't dwell into that any further
- ...setString( UCase(...getString()))to uppercase a cell