If you work with Excel spreadsheets regularly, you probably find yourself repeating the same steps over and over. Wouldn’t it be nice to click a button and have those tasks happen automatically?
That’s where Excel macros come in. You can use macros to automate repetitive tasks, which can save you a lot of time and effort.
What is excel macro?
An Excel macro is a recorded sequence of Excel commands and actions that you can play back as many times as you want. Macros can be used to automate almost any sequence of tasks in Excel, from entering your company’s name and address into a spreadsheet to something as complex as creating a custom report. If you can do it in Excel, you can probably automate it with a macro.
To use a macro, you first need Record This. You perform the sequence of steps you want to automate, and Excel tracks them all and saves them in a macro. After you record a macro, you can play it again at any time. You can also assign a keyboard shortcut to a macro, so you can run it with just a few keystrokes.
Excel macros are based on Microsoft’s Visual Basic for Applications (VBA) programming language. When you record a macro, Excel translates your actions into VBA code under the hood. So in addition to creating macros by recording them, you can also write them manually in VBA code. In this article, I’ll focus primarily on creating macros by recording them – the easiest and fastest way. After that, I’ll discuss how you can edit or write macros from scratch using VBA and provide some resources for self-learning.
how to record a macro
To help illustrate the process, I’ll use a small sample data set. Let’s say we’re responsible for taking customer names and balances and doing two things: first splitting the customer names into separate first and last names, and then highlighting all those who have a balance greater than zero. . In this example, we are given seven clients to work with.
To create the macro, we’ll use the Developer tab in the ribbon toolbar at the top of the Excel window. It is not present by default, so we have to add it. click on file Click on the tab on the far left of the ribbon (highlighted in the screenshot above) and then on the screen that appears alternative at the bottom of the left column.
The Excel Options screen appears. choose customize Ribbon from the left navigation bar. Then, in the “Customize the Ribbon” area on the right, look for the “Main Tab” list and check developer checkbox. Click Ok,
(In macOS, click Excel Menu at the top of the screen and select Preferences > Ribbon & Toolbars, In the “Customize the Ribbon” area on the right, look for the “Main Tabs” list and check developer checkbox. Click save,
Once you have the Developer tab, click on it and you will see options similar to the ones shown on the screen below.
To start recording your first macro, click record macro button, and you will be presented with the options below. First, come up with a name for your macro, keeping in mind that you can’t use spaces. For readability, you may want to separate the words with something like , either ,, Add shortcut keys or descriptions if you’d like, but these aren’t necessary.
once you hit Ok, the icon should change to indicate that the macro is recording your actions. It is important that you do only what you want the macro to do and nothing else from this point until you click stop recording,
Now that the macro is recording, let’s get down to business. First, highlight the Balance Due column, then right-click and choose insert column, This will add a new column between the Customer Name and Remaining Outstanding columns.
Next, we’ll rename the column by replacing “Customer Name” with “First Name” and adding the title “Last Name” to the column we just created. Select the customer name in the first column (cells A:2 to A:8), select Information tab in the ribbon, select More text for columns command.
A wizard appears displaying the following options. choose demarcated and hit next,
Next, choose space Checkbox to specify that words separated by spaces should go into separate columns. Click next,
For the last option, keep everything as it is and hit finish,
You should have the following result with the first and last names in separate columns.
Finally, we need to highlight each customer with a balance greater than zero. Highlight all the data in the third column (cells C:2 to C:8) and then click Home > Conditional Formatting,
choose highlight cell rule and then more than, enter 0 and click Ok To highlight each customer with a balance greater than zero.
This should be the end result:
Now that you have completed the task sequence, go back developer tab and click stop recording, Your first Excel macro is complete.
Important notes about working with macros
If you want to run your macro again, just click macro button and it will be available for you to play. Or, if you’ve assigned a shortcut to the macro, just press the key combination to run it.
Note that you cannot save a spreadsheet with macros as a traditional .xlsx workbook. To avoid losing your macros, you must save it as an Excel macro-enabled workbook (.xlsm).
Once you make that change, whenever you want to work with a new data set, you can reopen the workbook and import the data you want to work with. . information tab and select get data, You’ll be able to import data from files, databases, and other online services.
Be aware that in many cases macros are disabled by default. This is because, as Microsoft notes, “VBA macros are a common way for malicious actors to gain access to deploy malware and ransomware.” To protect organizations from such threats, Microsoft now blocks macros In files from the Internet – and sometimes in files stored on company shared drives. Your organization may have additional restrictions on macros.
So whenever you open an Excel workbook with macros (including your own), it is likely that you will see a warning message like the one below. If your workbook is one that you created or from a trusted source, go ahead and enable it. If the macro is from an untrusted source, however, do not enable it, as it may be malware.
Another important feature to be aware of when recording macros is the use of relative references. This feature makes it so that no matter where the data starts on the spreadsheet, the macro will be able to find it and begin processing there.
For example, the macro we created would always start processing on column A because relative references were not turned on. However, if we performed the same operations but clicked use relative references First, then the macro will be able to find where the information starts (e.g. column C) and start processing it from that point. This feature is useful if the data you’re working with won’t always start at the same point.
How to edit or create a macro with code
If you want to see the VBA code behind the macro, go here developer tab click macroSelect the macro and then click edit,
You will be taken to a pane where you can view the source code for the macro you created. In the screenshot below, the underlined items show the actions we want to take, such as renaming headings and selecting rows. You can change these for different use cases. For example, if the data you work with runs from the range A1:A20, you may want to expand the range to include all possible cells.
If you want to try your hand at writing macros from scratch, there are many resources online to learn how to write VBA scripts, including codevars, UdemyAnd codecademy, Back in Excel, click Developer > Macros > Create, You will be taken to a blank pane where you can write VBA code.
Copyright © 2022 IDG Communications, Inc.