Site Tools


software:libreoffice:calc:macros:basic:button

This is an old revision of the document!


Using buttons within calc to perform basic function

This describes how to write some code and setup your worksheet to perform a certain action when a button is pressed.

Create a button in cell

View→Toolbars→Form controls
Go into design mode (This disables any button functions) Select from the form controls Push Button
Draw this button in a cell.

Configure button

Change the label to something meaningful to the user:

  • Right click button→ click on “Control” - this will open the “Properties” dialog → select the “Label” box on the “General” tab - this is used to set the text to be displayed on the button
  • Assigning a macro to the button→click on the “Events” tab in the “Properties” dialog→click on the button on the same row as “Mouse button pressed” - this opens the “Assign action” dialog→click on “Macro” - this opens the “Macro Selector” dialog, and this should be used to select the correct macro for the button

Exit design mode

All buttons will be useless to a user of the spreadsheet if the design mode is active. To change from design mode to active mode two more steps are required:

  • the design mode is turned off by clicking the “Design Mode On/Off” icon (showing a pencil, ruler and set square) in the “Form Controls” toolbar

Basic code example

Paste code below into: Tools→Macros→Organize Macros→LibreOffice.org Basic…

REM Sheet containing the parts count report view
Public Const SHEET_NAME As String = "NAME_OF_THIS_SHEET"

Sub telop
    Dim oDoc As Object
    Dim oSheet As Object
    Dim oCell_1 As Object
    Dim oCell_2 As Object
    Dim cell As Object
    oDoc = ThisComponent
    oSheet = oDoc.getSheets().getByName(SHEET_NAME)
    oCell_1 = ThisComponent.Sheets(0).getCellRangeByName("A5")
    oCell_1.Value = 100
    oCell_2 = ThisComponent.Sheets(0).getCellRangeByName("A6")
    oCell_2.Value = 200
    Cell = thisComponent.Sheets(0).getCellByPosition(0, 7) 
    Cell.Formula = "=SUM(A5:A6)"
End Sub

Further reading

software/libreoffice/calc/macros/basic/button.1642606885.txt.gz · Last modified: 2022/01/19 16:41 by admin