The Big Excel Macro Special, Part 2: How to programme macros
In our first Macro Special, we explained what macros are and how to use them. Here, we’ll show you how to programme macros yourself!
- Published
- Content reviewed
Listen to this article
Start reading aloud with a voice from your device. Changing the voice, speed or section stops playback; you can then start again.
Checking available voices.
Overview
Excel, the programme in the Microsoft Office suite that is often somewhat overlooked, is incredibly useful and versatile. Most people probably only use Office, Word and, occasionally, PowerPoint in their day-to-day work. But once you’ve got to grips with Excel, you won’t want to be without its many features.
There is probably no other programme that is as handy as Excel for sorting, organising and presenting data. Whether it’s a never-ending spreadsheet for a new product or a shopping list, Excel is the right programme for the job.
Many users find themselves carrying out the same tasks over and over again in Excel. Data is imported, processed, sorted and then displayed. To avoid having to carry out these repetitive steps manually in Excel time and time again, there is a handy feature that allows you to combine these recurring sequences of steps into a macro.
In the first part of our Excel macro special, we explained in detail what macros are, what they can do and which tasks they are unsuitable for. Here is the practical section: how to programme an Excel macro.
Enable the macro function in Excel
To use macros in Excel at all, you must first enable the macro function in Excel. This is because the relevant button is often not displayed by default in Excel. To do this, click on ‘File’ in the ribbon and then on ‘Options’. In the Options window, go to the ‘Customise Ribbon’ section.
In the ‘Customise Ribbon’ window, select the ‘Main Tabs’ option from the menu in the top right-hand corner and tick the box next to ‘Developer’ in the bottom right-hand corner of the window. Excel will now automatically add the ‘Developer’ tab to the ribbon.
To record a new macro, you can now either switch to the ‘Developer Tools’ tab and click ‘Record Macro’ in the ‘Code’ pane, or open the ‘Macro’ function using the keyboard shortcut ALT+F8.
How to create a new macro in Excel
When you click on ‘Record Macro’, the ‘Record Macro’ window opens. Here you can create a new macro. First, you need to give the macro a name. Please note that a macro’s name must begin with a letter and spaces are not permitted. You can also assign a keyboard shortcut to the macro. Make sure that the key combination is not already in use elsewhere. The combination must start with the CTRL key. You should therefore not set the key combination to CTRL+V. You can also hold down multiple keys, such as Shift.
In our example, we’ll call the macro ‘Example_Macro’, assign the keyboard shortcut CTRL+SHIFT+B to it, and save the macro in ‘this workbook’.
You can also save the macro in a personal macro workbook or a new workbook. If you select the ‘personal macro workbook’ option, you will always be able to access the macro; Excel will then create a special workbook called ‘personal.xlsb’.
In the description field, you can enter what the macro is intended to do. It’s particularly important to keep this field up to date once you’ve created several macros – otherwise you’ll eventually lose track of them. Once all the fields have been filled in, confirm the action by clicking ‘OK’.
Record the functions for the new macro
Our example is a fictitious list of entries from an accounting programme. The macro is designed to sort the entries by value, display duplicate values and highlight negative values in red.
Once the macro has been created, a small square appears at the bottom edge of the Excel window, just like on a CD player. To record the macro, carry out the steps that you want the macro to perform later. It is important to work carefully here, so that Excel does exactly what you want it to do later on.
So let’s carry out the steps in exactly the right order.
- The columns for ‘Net amount’ and ‘Gross amount’ are formatted as ‘Currency’ accordingly.
- We then apply the ‘Duplicate values…’ conditional formatting to each of the two columns individually.
- We will then also introduce conditional formatting so that values greater than 0 are highlighted in red.
- Finally, the table should be converted into a smart table and sorted in descending order by gross amount.
To stop recording the macro, simply click on the small square.
Does the macro work?
To check whether the macro works, you can simply undo the steps you’ve just carried out and run the macro.
Bear in mind that once a macro has been run, it cannot be undone! You should therefore carry out a few test runs on an unimportant file first, before applying your new macro to an important file!
To run a macro, click on the ‘Developer’ tab and then on ‘Macros’, or use the keyboard shortcut ALT+F8 or the assigned shortcut (in our case, CTRL+SHIFT+B). In the ‘Macro’ window, click ‘Run’ and watch as Excel carries out all the assigned tasks as if by magic. Depending on the size of the macro or the tasks it contains, it may take a moment to run.
What if a mistake has crept in?
If you now realise that a mistake has crept into the macro, you have two options. Either you re-record the macro (which, depending on the situation, can be quite time-consuming) or you edit the macro. To do this, you’ll need at least a basic knowledge of English.
To edit the macro, open the ‘Macro’ window, click the ‘Edit’ button and go through the macro’s code line by line. In Excel, macros are programmed using VBA, the Office programming language. This is very straightforward, and to find individual errors, it’s usually enough simply to draw on your knowledge of English.
Errors cannot be deleted in this window. You can only exclude the relevant line from editing. To do this, place an apostrophe ‘ at the start of the line.
Conclusion: Excel macros are simple and practical
The example we have chosen to demonstrate how a macro works has been deliberately kept simple. Once you have understood the basics of a macro, you can expand it as you wish. You can include any function that can be used in Excel in a macro.
Macros are incredibly handy, especially if you find yourself carrying out the same tasks over and over again in Excel, and they save you a lot of work.
Credit for cover image: Dzmitry Kliapitski (123rf.com)
About the authors
Wiresoft Editorial Team
The Wiresoft editorial team explains software, licences and how to use Windows, Office and servers in a clear and practical way. The articles are based on the questions that customers ask Wiresoft in their day-to-day lives.
Sources and further reading
Matching productsOffice