The Big Excel Macro Special, Part 2: How to Program Macros
In our first Macro Special, we explained what macros are and how to use them. Here, we'll show you how to program 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 often-overlooked program in the Microsoft Office suite, is incredibly useful and versatile. Most people probably only use Office, Word, and sometimes PowerPoint in their day-to-day work. But once you’ve gotten the hang of Excel, you won’t want to do without its many features.
There's probably no other program as handy as Excel for sorting, organizing, and presenting data. Whether it's a never-ending spreadsheet for a new product or a shopping list, Excel is the right program for the job.
Many users perform the same tasks over and over again in Excel. Data is imported, processed, sorted, and then displayed. To avoid having to manually repeat these same steps in Excel time and time again, there is a convenient 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 aren't suitable for. Here's the practical part: how to program an Excel macro.
Enable the macro function in Excel
To use macros in Excel at all, you must first enable the macro feature in Excel. This is because the corresponding button is often not displayed by default in Excel. To do this, click “File” on the ribbon and then click “Options.” In the Options window, go to the “Customize Ribbon” section.
In the "Customize Ribbon" window, select the "Main Tabs" option from the menu in the upper-right corner and check the box next to "Developer" in the lower-right 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 ALT+F8 keyboard shortcut.
How to Create a New Macro in Excel
When you click “Record Macro,” the “Record Macro” window opens. Here you can create a new macro. First, you need to give the macro a name. Keep in mind that a macro’s name must start with a letter, and spaces are not allowed. You can also assign a keyboard shortcut to the macro. Make sure the shortcut isn’t already assigned to another function. The shortcut must start with the CTRL key. So you shouldn’t set the shortcut to CTRL+V. You can also hold down multiple keys, such as Shift.
In our example, we'll name 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'll always be able to access the macro; Excel will then create a special workbook named "personal.xlsb."
In the description field, you can enter what the macro is supposed to do. It’s especially 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 are filled out, confirm the process by clicking “OK.”
Record the functions for the new macro
Our example is a fictional list of entries from an accounting program. 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 of the Excel window, similar to the one on a CD player. To record the macro, perform the steps that you want the macro to carry out later. It’s important to work carefully here so that Excel does exactly what you want it to do later.
So let's follow the steps in exactly the right order.
- The columns for Net Amount and Gross Amount are formatted as "Currency" accordingly.
- Next, we’ll enable the “Duplicate Values…” conditional formatting for each of the two columns individually.
- Next, we'll also set up 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, just click on the small square.
Does the macro work?
To check whether the macro works, you can simply undo the steps you just performed and run the macro.
Keep in mind that once a macro has been run, it cannot be undone! So you should first run a few test runs on an unimportant file before applying your new macro to an important file!
To run a macro, click the “Developer” tab and then “Macros,” 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 magically performs all the assigned tasks on its own. Depending on the size of the macro or the tasks it contains, execution may take a moment.
What if a mistake has slipped in?
If you realize now that a mistake has crept into the macro, you have two options. Either you re-record the macro (which can be quite time-consuming, depending on the situation), or you edit the macro. To do this, you’ll need at least some 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. It’s very simple, and to find individual errors, it’s usually enough to rely on your knowledge of English.
Errors cannot be deleted in this window. You can only exclude the corresponding line from editing. To do this, place an apostrophe ‘ at the beginning of the line.
Conclusion: Excel macros are simple and practical
The example we've chosen to demonstrate how a macro works is intentionally simple. Once you understand the basics of a macro, you can expand it however you like. You can include any function that works in Excel in a macro.
Macros are incredibly handy—especially if you find yourself performing the same tasks over and over again in Excel—and they save you a lot of work.
Cover photo credit: Dzmitry Kliapitski (123rf.com)
About the authors
Wiresoft Editorial Team
The Wiresoft editorial team explains software, licenses, and how to use Windows, Office, and servers in a clear and practical way. The articles are based on questions that customers ask Wiresoft in their day-to-day lives.
Sources and further reading
- Microsoft Support: Help and Learning for ExcelSource date:
Matching productsOffice