Using Macros in Spreadsheet
This chapter explains how macros can be used to automate repeated tasks in LibreOffice Calc.
βοΈ NOTES ONLY β’ NO TOP 50 TESTSource: NCERT Domestic Data Entry Operator β Class X, 2023β24, Part B, Unit 2, Chapter 5.
πΊοΈ Chapter Roadmap
- Introduction to Macros
- Recording a Macro
- Enabling Macro Recording
- Actions Not Recorded by Macro
- Saving a Macro
- Macro, Library and Module
- Rules for Naming Macro / Module / Library
- Running a Macro
- Editing Macro Code
- Creating and Organising a Simple Macro
- Macro as a Function
- Arguments and Return Value
- Using a Macro as a Function in Calc
Many spreadsheet tasks are performed repeatedly. For example, entering the same school name, address or contact information with a particular format, or applying the same formula to different sheets.
Performing such tasks manually again and again can take time and may lead to typing or formatting mistakes.
A Macro can be used to automate repeated tasks that are performed in the same way repeatedly.
Definition of Macro
A macro is a single instruction that executes a set of instructions.
These instructions may be a sequence of commands or keystrokes that can be used any number of times later.
Macro is a single instruction that executes a set of instructions.
- Automates repeated tasks.
- Saves time.
- Reduces repeated manual work.
- Helps maintain standardisation.
- Reduces typing mistakes in repeated tasks.
Repeated Task β Record Once β Run Again
By default, the macro recording feature is turned off when LibreOffice is installed.
Therefore, before recording a macro, macro recording must be enabled.
Steps to Enable Macro Recording
- Click Tools on the main menu bar.
- Select Options.
- Select LibreOffice.
- Select Advanced.
- Under Optional Features, locate Enable macro recording.
- Put a check mark on Enable macro recording.
Tools β Options β LibreOffice β Advanced
Once macro recording has been enabled, the Record Macro option becomes available under the Tools β Macros menu.
Procedure
- Click Tools β Macros β Record Macro.
- Start performing the actions that you want to record.
- When recording starts, a small alert is displayed.
- Click Stop Recording when the required actions are completed.
- The Basic Macros dialog box opens.
- Select the object where the macro has to be saved.
- Give the macro a suitable name.
- Click Save.
Tools β Macros β Record Macro
The Macro Recorder records keyboard and mouse actions performed during the recording.
However, some actions are specifically not recorded.
Actions NOT Recorded
- Opening of windows.
- Actions carried out in another window from where recording was started.
- Window switching.
- Actions unrelated to spreadsheet contents.
- Changes made in the Options dialog.
- Actions in Macro Organizer.
- Customizing actions.
Important: Selections are recorded only when they are made using the keyboard/cursor movement, not when the mouse is used for selection.
The macro recorder works only in Calc and Writer.
After recording is stopped, the Basic Macros dialog appears.
The recorded macro can then be saved in the required object/library.
Important Points
- A Standard Library is present by default when a spreadsheet is created and saved.
- The default macro name is Main.
- By default, Main is saved in the Standard Library β Module1.
- The macro name can be changed.
Default Macro Name β Main
Default Library β Standard Library
Default Module β Module1
Library
A Library is a collection of modules.
Module
A module contains macros. Therefore:
A Library is a collection of modules, which in turn is a collection of macros.
The chapter specifies rules that must be followed while naming a Macro, Module or Library.
| Rule | Requirement |
|---|---|
| First character | Name should begin with a letter. |
| Spaces | Spaces are not allowed. |
| Special characters | Special characters are not allowed except underscore (_). |
Examples
Format_Heading
Names such as the following are invalid:
format word
format*word
Format_word is valid because underscore is allowed.
format word is invalid because spaces are not allowed.
A previously created macro can be run whenever the same task is required again.
Procedure
- Type the required heading or data in the worksheet.
- Click Tools β Macros β Run Macro.
- The Macro Selector dialog box opens.
- Select the required library and module.
- Select the required macro from the Macro Name list.
- Click Run.
Tools β Macros β Run Macro
| Library | Meaning |
|---|---|
| LibreOffice Macros | Provided by LibreOffice and contains modules with prerecorded macros. It should not be changed. |
| My Macros | Contains macros that we write or add to LibreOffice. |
| Untitled1 | Name of the worksheet being worked on when it has not yet been saved with another name. |
LibreOffice Macros library contains prerecorded macros and should not be changed.
The actions recorded by a macro are stored as instructions in a programming language called BASIC.
BASIC is the programming language used for the recorded macro instructions in Calc.
Viewing Macro Code
The generated macro code can be viewed through:
The selected macro's associated code can be viewed in the IDE.
Macro code should not be changed unless you have knowledge of the language in which it is written.
The code of a macro begins with Sub followed by the name of the macro and ends with End Sub.
' Macro instructions
End Sub
Macro code begins with Sub and ends with End Sub.
Macros can also be organised manually by creating libraries and modules.
Opening Basic Macro Organiser
- Click Tools β Macros β Organize Macros β LibreOffice Basic.
- The LibreOffice Basic Macro dialog opens.
- Click Organizer.
- The Basic Macro Organizer opens.
Creating a Library
To create a new library containing modules:
The newly created library can then be used to organise modules and macros.
Tools β Macros β Organize Macros β LibreOffice Basic
The chapter introduces the IDE for viewing and editing macro code.
IDE stands for Integrated Development Environment.
The IDE displays the programming code associated with the macro.
IDE = Integrated Development Environment
The chapter also explains that a macro can be used as a function in Calc.
A macro used as a function can accept arguments/values and return a result/value.
Macro as Function β Accepts arguments/values β Returns result/value
Difference from Normal Macro
| Macro | Macro as Function |
|---|---|
| Executes a set of recorded/programmed instructions. | Can accept input values and return a result/value. |
| Used mainly for automation of tasks. | Can be used in a spreadsheet formula-like manner. |
Arguments / Values
Arguments are the input values supplied to a macro function.
Result / Value
The return value is the result produced by the macro function.
A macro as a function is capable of accepting arguments/values and returning a result/value.
The textbook demonstrates a macro as a function using a calculation for the number of tiles required.
The macro function accepts required values and returns the calculated result.
The important concept is not merely the example, but the way a macro can be designed to accept values and return a result.
The chapter also covers execution of a module from the IDE.
A module can be executed from the IDE by pressing F5.
| Term | Exam-Oriented Meaning |
|---|---|
| Macro | Single instruction that executes a set of instructions. |
| Macro Recorder | Records a sequence of actions. |
| Library | Collection of modules. |
| Module | Contains macros. |
| BASIC | Programming language used for macro instructions. |
| IDE | Integrated Development Environment. |
| Macro Selector | Dialog used to select and run a macro. |
| Standard Library | Default library in which the default macro is saved. |
| Sub | Beginning of macro code followed by macro name. |
| End Sub | End of macro code. |
| Task | Menu Path |
|---|---|
| Enable Macro Recording | Tools β Options β LibreOffice β Advanced |
| Record Macro | Tools β Macros β Record Macro |
| Run Macro | Tools β Macros β Run Macro |
| Edit Macro | Tools β Macros β Edit Macros |
| Organise Macros | Tools β Macros β Organize Macros β LibreOffice Basic |
- Macro recording is OFF by default.
- Macro recording can be enabled from Tools β Options β LibreOffice β Advanced.
- The option to enable is Enable macro recording.
- The default macro name is Main.
- Default storage is Standard Library β Module1.
- A library is a collection of modules.
- A module contains macros.
- Macro instructions are written in BASIC.
- Macro code begins with Sub and ends with End Sub.
- IDE stands for Integrated Development Environment.
- A macro as a function accepts arguments/values and returns a result/value.
- F5 can be used to execute a module from the IDE.
- Macro names cannot contain spaces.
- Special characters are not allowed except underscore (_).
| Question Type | Answer to Remember |
|---|---|
| What is a Macro? | Single instruction executing a set of instructions. |
| Default macro recording status? | Turned off. |
| Programming language? | BASIC. |
| Default macro name? | Main. |
| Default library? | Standard Library. |
| Library contains? | Modules. |
| Module contains? | Macros. |
| Macro code begins with? | Sub. |
| Macro code ends with? | End Sub. |
| IDE full form? | Integrated Development Environment. |
| Macro function accepts? | Arguments / values. |
| Macro function returns? | Result / value. |
| IDE module execution key? | F5. |
The textbook's Check Your Progress for Chapter 5 focuses on the following areas:
- Macro definition.
- Uses of macros.
- Actions not recorded by Macro Recorder.
- Difference between LibreOffice Macros and My Macros.
- Macro as a function.
- Rules for naming macros.
- Advantages of macros.
- Default library.
- IDE full form.
- Arguments and return values.
- Macro Organizer.
- Sub and End Sub.
These topics are directly reflected in the chapter's own Check Your Progress and answer key, so they should be given high priority while preparing for the examination.
β‘ CHAPTER 5 β RPAID REVISION
- Macro β Single instruction that executes a set of instructions.
- Purpose β Automate repeated tasks.
- Default recording β OFF.
- Enable recording β Tools β Options β LibreOffice β Advanced.
- Record Macro β Tools β Macros β Record Macro.
- Run Macro β Tools β Macros β Run Macro.
- Edit Macro β Tools β Macros β Edit Macros.
- Organize Macros β Tools β Macros β Organize Macros β LibreOffice Basic.
- Default Macro Name β Main.
- Default Library β Standard Library.
- Default Module β Module1.
- Library β Collection of modules.
- Module β Collection/container for macros.
- BASIC β Programming language for macro code.
- IDE β Integrated Development Environment.
- Macro Code β Sub ... End Sub.
- Macro as Function β Arguments/values β Result/value.
- IDE execution β F5.
- Naming Rule β Begin with a letter.
- No spaces in Macro, Module or Library names.
- Special characters are not allowed except underscore (_).
- LibreOffice Macros β Prerecorded macros; should not be changed.
- My Macros β Macros written or added by us.