This guide shows you how to create and run a macro in LibreOffice Calc on Linux. You’ll learn to record a simple macro, open the Basic IDE, write or edit code, and execute it via a keyboard shortcut or a button. No prior programming experience is needed, but familiarity with the Calc interface helps.
What You’ll Need
- A Linux distribution with LibreOffice installed (most have it pre-installed).
- A spreadsheet document in LibreOffice Calc (.ods or any other format).
- Basic understanding of the Calc interface (menus, toolbars).
Step-by-Step
- Open LibreOffice Calc – Launch Calc from your application menu or terminal by typing
libreoffice --calc. - Enable Macro Recording (if needed) – Go to Tools > Options > LibreOffice > Advanced and ensure Enable macro recording is checked. This is usually enabled by default.
- Record a Macro (optional but easy) –
- Go to Tools > Macros > Record Macro.
- Perform the actions you want to automate (e.g., formatting a cell, inserting text).
- Click Stop Recording. A dialog appears asking where to save the macro.
- Choose My Macros (for personal use) or the current document, give it a name, and click Save.
- Open the Basic Macro Editor – Go to Tools > Macros > Organize Macros > Basic. In the dialog, select the macro you recorded (or create a new one by clicking New), then click Edit. This opens the LibreOffice Basic IDE.
- Write or Edit the Macro Code – In the IDE, you’ll see the module structure. The recorded code appears automatically. You can modify it or write new code. For example, to set the value of cell A1 to “Hello”, use:
Sub HelloWorld ThisComponent.Sheets(0).getCellByPosition(0,0).String = "Hello" End Sub
- Save the Macro – Press Ctrl+S in the IDE to save your changes. If you’re saving into the document, make sure to save the document itself afterward (Ctrl+S in Calc).
- Run the Macro – There are several ways:
- From the menu: Go to Tools > Macros > Run Macro, select your macro, and click Run.
- From the Basic IDE: Place the cursor inside the macro and press F5.
- Via a keyboard shortcut: Go to Tools > Customize > Keyboard, find your macro under Category > Macros, assign a key combination, and click OK.
- Via a button: Use View > Toolbars > Form Controls to insert a push button, then right-click the button, select Properties, and set the Action to the macro.
- Test the Macro – After running, check that the expected changes occurred in your spreadsheet. If not, return to the IDE and debug.
Common Mistakes
- Not saving the macro in the right location: If you save a macro in the document but then open a different document, the macro won’t be available. Use “My Macros” for global access.
- Forgetting to enable macros: LibreOffice may block macros for security. Go to Tools > Options > Security > Macro Security and set to “Medium” or “Low” (use Low only for trusted documents).
- Using the wrong object names: In Basic, the sheet index starts at 0, and cell positions are (column, row) with 0-based indices. Mixing up these indices is a common source of errors.
- Not closing the Basic IDE before running: Sometimes the macro runs in the IDE context, not the document. Make sure the document is the active window when you run via menu.
Tips
- Use the recorder for simple tasks: It generates correct syntax that you can then modify.
- Learn basic LibreOffice Basic syntax: Variables, loops, and conditionals follow a structure similar to Visual Basic. For example,
Dim i As Integer. - Access cells via the API: Use
ThisComponent.Sheets(0).getCellByPosition(col, row)for reliable access. - Assign macros to toolbar buttons: This makes them one-click accessible. Use Tools > Customize > Toolbars.
- Store reusable macros in “My Macros”: They become available in all documents, saving time.
Troubleshooting
- Macro doesn’t appear in the list: If you saved it in a document, make sure that document is open. If it’s in “My Macros”, check that you saved it in the correct library (usually “Standard”).
- “Basic not found” error: This means the macro library isn’t loaded. Go to Tools > Macros > Organize Macros > Basic, click Libraries, and ensure the library is marked as “installed”.
- Macro runs but does nothing: Check the code for logical errors. Use F8 in the IDE to step through the code and inspect variable values.
- Security warning when opening the document: If you see a warning about macros, click Enable Macros if you trust the source. To avoid this, set macro security to “Medium” and you’ll be prompted each time.
- Keyboard shortcut doesn’t work: Make sure the shortcut isn’t already assigned to another function. In the Customize dialog, the current assignment is shown; choose a free combination.
Frequently Asked Questions
Can I record a macro in LibreOffice Calc?
Yes, go to Tools > Macros > Record Macro. Perform actions, then stop and save. The recorded code is editable in the Basic IDE.
Where are macros stored?
Macros can be stored in the document itself (embedded) or in the user’s macro library (“My Macros”). The latter is available globally.
How do I run a macro automatically when a document opens?
You can assign a macro to the document’s “Open Document” event via Tools > Customize > Events.
Can I use Python instead of Basic for macros?
Yes, LibreOffice supports Python macros, but the setup is more complex. You need to enable the Python script provider and write scripts in the appropriate directory.
Why is my macro not working on another computer?
If the macro is embedded in the document, it should work. If it’s in “My Macros”, you need to export the library (Tools > Macros > Organize Macros > Basic > Libraries > Export) and import it on the other machine.
Is there a way to debug macros?
Yes, in the Basic IDE you can set breakpoints by clicking in the left margin, then run with F5. Use F8 to step through lines and F7 to step into functions.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix Missing Scroll Bar in Chrome
- Fix Sony Vegas Crashing on Windows 10
- Fix Outlook Sent Items Missing? Quick Solutions
- Hide WhatsApp Channel: Android & iOS Guide
- Slow Context Menu in Windows 11: Fixes
- Declutter Safari on iPhone: 15 Quick Tips