5
CHAPTER
Using Macros in Spreadsheet
IntroductIon
Many times, we have to perform repeated tasks such
as typing school name, address, contact numbers with
a specific formatting or apply the same formula at a
particular cell for different sheets in a workbook. How
easy would it be if we could get this done at the click
of a button? This will ensure that we maintain the
standardization in terms of font style without any typing
mistake. Can you think of any other advantage? 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. A sequence of actions such as
keystrokes and clicks can be recorded and then run as
per the requirement.
It is important to know that by default
the macro recording feature is turned
off when LibreOffice is installed on your
computer. Hence, to record a Macro ensure
that the macro recording is enabled. Macro
recording can be enabled using the Tools
option on the main menu bar 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
Fig. 5.1: Enabling a Macro macro recording” as shown in Fig. 5.1.
2023-24
Observe in Fig. 5.2, that the Record
Macro option found under Tools > Macros
is visible now.
The Macro records all the keyboard and
mouse actions but the following actions
are not recorded.
• Opening of windows
• Actions carried out in another
window than where the recording
was started.
• Window switching
• Actions that are not related to the
spreadsheet contents. For example,
Fig. 5.2: Record Macro option after enabling macro
changes made in the Options dialog,
macro organizer, customizing.
• Selections are recorded only if they are done by
using the keyboard (cursor traveling), but not
when the mouse is used.
• The macro recorder works only in Calc
and Writer.
Follow the steps given below to record a macro.
Step 1. Click on Tools > Macros and then click on
the Record Macro option.
Step 2. Now start taking actions that will
be recorded.
Step 3. Once you click on Record Macro option,
recording of actions starts and a small
alert will be displayed as shown in Fig. 5.3. Fig. 5.3: Stop Recording button
Clicking on “Stop Recording” button will
stop the recording of actions.
Step 4. This will open the Basic Macros dialog
window to save and run the created macro,
as shown in Fig. 5.4.
Step 5. To save the macro, first select the object
where you want the macro to be saved in
the Save Macro to list box.
Step 6. The name of the macro by default is Main
and is saved in the Standard Library in
Module1. You can change the name of
the macro.
Fig. 5.4: Basic Macros dialog
Step 7. Click on Save button. window
ElEctronic SprEadShEEt (advancEd) uSing librEofficE calc 105
2023-24
notes A Library is a collection of modules which in turn is a
collection of macros.
Practical Activity 5.1
Record a macro to apply the following style to the Heading “Data
Analysis” in the cell A1.
The font style should be “Times New Roman”
The font size should be “14”
The font colour should be “Blue”
Cells from A1 to G1 should be merged and centred
Procedure
Step 1. Create the spreadsheet. Write the heading “Data
Analysis” in cell A1.
Step 2. Choose Tools > Macros > Record Macro. Perform the
actions mentioned above in Practical Activity 5.1 in
the spreadsheet.
Step 3. Click Stop Recording to stop the Macro Recorder.
Step 4. The Basic Macro dialog appears as shown in Fig. 5.4,
in which you can save and run the macro.
Step 5. A Standard library is present by default when a
spreadsheet is created and saved.
Step 6. To save the macro, first select the object where you
want to save the macro in the Save macro in list
box. Fig. 5.5 shows that the Main macro is saved by
default in the Standard Library in Module1. You can
change the name of the macro. Here, we have named
the macro as FormatHeading.
Fig. 5.5: Naming a Macro
Note : If all the macros will be given the same name then they
will overwrite the previous Macro created by that name.
106 Domestic Data entry operator – class X
2023-24
Rules for naming a Macro, Module or a Library: notes
While naming a Macro, Module or a Library the name
should :
• Begin with a letter
• Not contain spaces
• Not contain special characters except for _
(underscore)
Running a Macro
Let us now run the macro for another sheet which
requires its heading in cell A1 to be given the same
format as recorded in the Formatheading macro. To run
a macro we need to perform the following steps.
Step 1. Type the heading in cell A1
Step 2. Use Tools > Macros > Run Macro to open the
Macro Selector dialog box as shown in Fig. 5.6.
Fig. 5.6: Selecting and runnig a Macro
Fig. 5.6 shows three names in the library. Out of which
• LibreOffice Macros library is provided by
LibreOffice and contains modules with pre
recorded macros and should not be changed.
• My Macros contain macros that we write or add
to LibreOffice.
• Untitled1 is the name of the worksheet we are
working on. Since at this time we have not saved
ElEctronic SprEadShEEt (advancEd) uSing librEofficE calc 107
2023-24
notes the worksheet with another name it is showing
Untitled1. You will see further that Untitled1 will
be replaced with the name of the spreadsheet.
Step 3. Select the library and module in the Library
list.
Step 4. Select the macro in the Macro name list.
Step 5. Click Run to run the macro.
Practical Activity 5.2
Run a previously created Macro Formatheading.
Step 1. Type the heading in cell A1.
Step 2. Open the Macro selector dialog box by clicking Tools
-> Macros-> Run Macro
Step 3. Click on MyMacros > Standard > Module1. Then
select Formatheading from the Macro Name box and
click Run as shown in Fig. 5.6.
You will observe that the Heading in Cell A1 is formatted.
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 in which
the macro is written which is BASIC in case of CALC. You 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 as shown in Fig. 5.7.
Fig. 5.7: Code generated for macro in IDE
Warning – Do not make any changes to the code unless you are
aware of the language.
Note – The code of a macro begins with Sub followed by the
name of the macro and ends with End Sub .
108 Domestic Data entry operator – class X
2023-24
Creating and Organising a Simple Macro
Till now we have learned to record, store and run the
macro. The recorded macro is internally stored as
instructions written in a programming language that
are executed when the macro is executed or run.
We selected a library or module to store our recorded
macro, similarly while creating a macro, either create a
new library/module or edit an existing module stored in
a library. Use the following steps to organize the macro.
Step 1. Click on Tools > Macros > Organize Macros >
LibreOffice Basic to open the LibreOffice Basic
Macro dialog window as shown in Fig. 5.8.
Step 2. Click Organizer to open the Basic Macro
Organizer dialog as shown in Fig. 5.9.
Fig. 5.8: Basic Macro dialog window Fig. 5.9: LibreOffice Basic Macro Organiser
Create a library to store a macro
To create a new library containing modules, click on
Library > New.
Use the following steps to create a module to store
a macro.
Choose the Modules Tab and select a Module. Click
on New to create a new Module.
ElEctronic SprEadShEEt (advancEd) uSing librEofficE calc 109
2023-24
notes
Practical Activity 5.3
Create a simple Macro using BASIC programming instructions
that will display Hello in a dialog box stored in the Standard
module as shown in Fig. 5.10 when executed.
Fig. 5.10: A Dialog Displaying Hello using macro
Step 1. Open the LibreOffice Basic Macro dialog box using
Tools > Macros > Organize Macros > LibreOffice
Basic.
Step 2. From the Basic Macros dialog box select Organizer
Step 3. From Modules tab, select My Macros folder and click
on New button as shown in Fig. 5.11.
Fig. 5.11: Creating a Module with name Hello
Step 4. Give a name to the New Module say ‘Hello’ and click
Ok. You will see Hello under Standard Library in My
Macros Library.
Fig. 5.12: Module Hello in Basic Macro Organizer Dialog
Step 5. Select Hello and click Edit (Fig. 5.12) to open the
Integrated Development Environment (IDE). It is a
text editor that allows you to create and edit macros.
Type Print “Hello” between Sub Main and End Sub as
shown in Fig. 5.13.
110 Domestic Data entry operator – class X
2023-24
notes
Fig. 5.13: Code of Hello Macro
The created module can be executed as explained in the previous
section. Besides, the module can be executed from the IDE by
either clicking the Run button or pressing F5.
Macro as a Function
Consider a situation wherein you need to perform
calculations that are repetitive in nature. Assume that
the same formula needs to be applied to different data in
different sheets and there is no predefined function for it.
In such a situation will it not be convenient if we could
create a macro that performs the calculations? It will save
us the 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. It can perform operations on the arguments,
perform calculations and return the result.
Practical Activity 5.4
Calculate the number of tiles required for flooring of a room
using macro as a function. The length and width of the room and
the length and width of a tile is entered in the sheet as shown
in Fig. 5.14.
Fig. 5.14: Sheet storing length and width of floor and tile
ElEctronic SprEadShEEt (advancEd) uSing librEofficE calc 111
2023-24