, 2 min read
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, Sheet3Cells(i,j)references i-th row, j-th column, e.g.,Cells(2,3)is C2Cells(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 cellMsgBox(string)for displaying a short text in a message boxIsEmpty(cell)checks whether cell is emptyWorksheets(i).Cells.Interior.ColorIndex=xlNoneto clear background color in all cells in a worksheetWorksheets(i).Cells.ClearCommentsto clear all comments in all cells in a worksheetWorksheets(i).Cells.Clearto wipe out all content in a worksheetWorksheets(i).Activatemakes i-th worksheet active (bring to foreground)Application.Dialogs(xlDialogSaveAs).Show "filename.csv", xlCSVopen file-save-as dialogUCase(...)return upper case of argument
The usual BASIC-style statements are used, e.g.,
for i=1 to n ... nextif ... then ... else ... end ifselect case variable case value ...end selectDim 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 Sheet1ThisComponent.getSheets().getByIndex(0).getCellByPosition(i,j).getString()fetching character string from cell at row (i+1), and column (j+1)...getValue()for the numeric valueThisComponent.getSheets().getByIndex(0).Annotations.insertNew( ThisComponent.getSheets().getByIndex(0).getCellByPosition(0,0).getCellAddress, "New comment" )for adding a commentThisComponent.getSheets().getByIndex(0).getCellByPosition(0,0).CellBackcolor = 48000sets the background colorMsgBox(string)as aboveThisComponent.getSheets().getByIndex(0).getCellByPosition(0,0).Type = 2to check if cell is emptyThisComponent.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