Chapter 5 Macros Notes
Chapter 5 Macros Notes
Macro in Spreadsheet
Introduction
Many times, we require to perform repeated tasks such as typing school name, address, contact numbers
witha specific formatting or apply the same formula at a particular cell for different sheets in a
workbook.
In this chapter, you will learn how to use a macro to automate repeated tasks that are always performed
in the same way over and over again.
Recording a Macro
A macro is a single instruction that executes a set of instructions. These set of instructions can be a
sequence of commands or keystrokes that can be used for any number of times later.
By default the macro recording feature is turned off when LibreOffice is installed on our computer.
Macro recording can be enabled by selecting Tools > Options > LibreOffice > Advanced. Observe the
Optional Features. There are two options which are not check marked. Put the checkmark on the option
“Enable macro recording”
Macros in Spreadsheet
Following actions are not recorded in Macro
1. Opening of windows
2. Actions carried out in another window than where the recording was started.
3. Window switching
4. Actions that are not related to the spreadsheet contents. For example, changes made in the Options
dialog, macro organizer, customizing.
5. The macro recorder works only in Calc and Writer.
NOTE: By default the name of the macro is Main and is saved in the Standard Library in Module1. A
Library is a collection of modules which in turn is a collection of macros.
Running a Macro
To run a macro we need to perform the following steps.
Step 1 : Click Tools > Macros > Run Macro
Step 2: Macro Selector dialog box will open.
Step 3 : Select the library that contains the macro then select the macro under ‘Macro name’.
Step 4: Click on Run to run the macro.
Code of a Macro
The action recorded by a macro is recorded as instructions in a programming language called BASIC. It
is also possible to view and thus edit the code of a macro. But remember, it is advised to edit a macro
only if you have knowledge of the language.
We can view the code generated for the macros by going to Tools > Macros > Edit Macros. Choose the
macro name from the Object Catalog and the associated code will be visible.
NOTE: The code of a macro begins with Sub followed by the name of the macro and ends with End
Sub. Do not make any changes to the code unless you are aware of the language.
Macro as a Function
Suppose we need to perform the same calculation again and again on different sheets and there is no
predefined function for it. In such a situation it will be convenient to create a macro that performs the
calculations. It will save our effort of remembering and typing the formulas.
It is possible to do so if we use Macro as a function. Instead of writing instructions in between Sub and
End Sub, we can write instructions in between Function and End Function. A function is capable of
accepting arguments or values