LIBRE OFFICE CALC
-Use Macros in Spreadsheet
Curriculum
• Using the macro recorder.
• Creating a simple macro.
• Using a macro as a function.
• Passing arguments to a macro.
• Passing the arguments as values.
• Macros to work like built- in functions.
• Accessing cells directly.
• Sorting the columns using macro
Macros
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.
Advantages of Macros
• Used to automate repeated tasks that are always performed in the
same way over and over again.
• Used to perform most of the redundant tasks with relative ease.
• Used for any number of times later
Enabling a Macro
• 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 by:
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”
• Observe that the Record Macro option found under Tools > Macros is visible now.
Macros cannot perform some actions:
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, 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.
Steps to create a Macro
• Click on Tools > Macros > Record Macro option.
• Now start taking actions that will be recorded.
• Click on “Stop Recording” button to stop the recording of actions.
• The Basic Macros dialog window appears in which you can save and run
the created macro.
• To save the macro, first select the object where you want the macro to be
saved in the Save Macro in list box.
• 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.
• Click on Save button.
Note : If all the macros will be given the same name then they will
overwrite the previous Macro created by that name
Library
• A Library is a collection of modules which in turn is a collection of
macros.
Rules for naming a Macro, Module or a Library
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)
To Run a Macro
1. Use Tools > Macros > Run Macro to open the Macro Selector dialog
box
There are 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
2. Select the library and module in the Library list.
3. Select the macro in the Macro name list.
4. Click Run to run the macro
Creating and Organising a Simple Macro
• We can record, store and run the macro.
• The recorded macro is internally stored as instructions written in a
programming language(BASIC) 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.
Editing the Code ( in BASIC) of 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 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.
NOTE: 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 .
Steps to organize the Macro
• Click on Tools > Macros > Organize Macros > LibreOffice Basic to open
the LibreOffice Basic Macro dialog window as shown in Fig. 5.8.
• Click Organizer to open the Basic Macro Organizer dialog
Create a library to store a macro:
To create a new library containing modules, click on Library > New.
Create a module to store a macro:
Choose the Modules Tab and select a Module. Click on New to
create a new Module
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.
Macro as a Function with zero argument
Function functionName()
Example –
Body of the Function
functionName=Result Function Total()
End Function Total=10+20
End Function
Calling a function with no argument
for example, =Total()
Macro as a Function with one argument
Function Example –
functionName(argument)
Body of the Function Function Circumference(radius)
functionName=Result Circumference=2*3.14*radius
End Function End Function
Calling a function with one argument
for example, =circumference(5)
Macro as a Function with two arguments
Passing one argument is as easy as Listing 6. TestMax accepts two arguments and
passing two: returns the larger of the two.
add another argument to the
function definition.
When calling a function with two
arguments, separate the
arguments with a comma
for example, =TestMax(3 , -4)
Sorting the columns using Macro
• Tools→ Macros→ Record Macros
• Select Range
• Data→ Sort
• Select the Sorting Category
• Stop Recording
• Save Macro
• Run Macro