Home Review How to use Excel macros to save time and automate your work

How to use Excel macros to save time and automate your work

0
How to use Excel macros to save time and automate your work

If you frequently work with Excel spreadsheets, you in all probability end up repeating the identical steps again and again. Wouldn’t or not it’s good to click on a button and have these duties occur routinely?That’s the place Excel macros are available in. You can use macros to automate repetitive duties, which may prevent a number of effort and time.What is an Excel macro?An Excel macro is a recorded sequence of Excel instructions and actions you could play again as many occasions as you need.  Macros can be utilized to automate nearly any sequence of duties in Excel, from one thing so simple as getting into your organization’s identify and tackle right into a spreadsheet to one thing as advanced as making a customized report. If you are able to do it in Excel, you’ll be able to in all probability automate it with a macro.To use a macro, you first must document it. You carry out the sequence of steps you need to automate, and Excel tracks all of them and saves them within the macro. Once you’ve recorded a macro, you’ll be able to run it once more at any time. You may even assign a keyboard shortcut to a macro, so you’ll be able to run it with just some keystrokes.Excel macros are primarily based on Microsoft’s Visual Basic for Applications (VBA) programming language. When you document a macro, Excel interprets your actions into VBA code below the hood. So along with creating macros by recording them, you can even write them manually in VBA code. In this text, I’ll focus totally on creating macros by recording them — the best and quickest methodology. After that, I’ll talk about how one can edit or write macros from scratch utilizing VBA and supply some sources for self-learning.How to document a macroTo assist illustrate this course of, I’ll use a small pattern information set. Let’s suppose that we’re answerable for taking buyer names and balances and performing two duties: first splitting the client’s identify into separate first and final names, after which highlighting everybody who has a steadiness due that’s larger than zero. In this instance, we’ve been given seven prospects to work with. Shimon Brathwaite/IDG

Our beginning information set. Note the File menu on the left finish of the Excel Ribbon. (Click picture to enlarge it.)

To create a macro, we’ll use the Developer tab within the Ribbon toolbar on the high of the Excel window. This isn’t current by default, so we might want to add it. Click on the File tab on the far left of the Ribbon (highlighted within the screenshot above) after which, on the display that seems, click on Options on the backside of the left column.The Excel Options display seems. Select Customize Ribbon from the left navigation bar. Then, within the “Customize the Ribbon” space on the proper, look within the “Main Tabs” checklist and verify the Developer checkbox. Click OK. Shimon Brathwaite/IDG

Check the Developer checkbox to activate the Developer tab within the Ribbon. (Click picture to enlarge it.)

(In macOS, click on the Excel menu on the high of the display and select Preferences > Ribbon & Toolbar. In the “Customize the Ribbon” space on the proper, look within the “Main Tabs” checklist and verify the Developer checkbox. Click Save.)Once you’ve gotten the Developer tab, click on on it and you will note choices much like these proven on the display beneath. Shimon Brathwaite/IDG

The highlighted instructions on the Developer tab make it easier to document and handle macros. (Click picture to enlarge it.)

To begin recording your first macro, click on on the Record Macro button, and you may be offered with the choices beneath. First, create a reputation on your macro, holding in thoughts you could’t use areas. For readability, chances are you’ll need to separate phrases with one thing like _ or -. Then add a shortcut key or description should you like, however these aren’t required. Shimon Brathwaite/IDG

To get began, give your macro a reputation. (Click picture to enlarge it.)

Once you hit OK, the icon ought to change to point that the macro is recording your actions. It’s vital that you just solely carry out the actions that you really want the macro to do and nothing else from this level till you click on on Stop Recording. Now that the macro is recording, let’s start our duties. First, spotlight the Balance Due column, then right-click and choose Insert Column. This will add a brand new column in between the Customer Name and Balance Due columns. Shimon Brathwaite/IDG

Insert a brand new column to the left of the Balance Due column. (Click picture to enlarge it.)

Next, we’ll rename the columns, changing “Customer Name” with “First Name” and including the heading “Last Name” to the column we simply created. Select the client names within the first column (cells A:2 to A:8), choose the Data tab within the Ribbon, and choose the Text to Columns command.A wizard seems displaying the next choices. Select Delimited and hit Next. Shimon Brathwaite/IDG

On the primary display of the Convert Text to Columns Wizard, choose Delimited. (Click picture to enlarge it.)

Next, choose the Space checkbox to designate that phrases separated by an area ought to go into separate columns. Click Next. Shimon Brathwaite/IDG

On display 2 of the wizard, choose Space as your delimiter. (Click picture to enlarge it.)

For the final possibility, preserve every part the identical and hit Finish. Shimon Brathwaite/IDG

Don’t make any modifications to the final display of the wizard. (Click picture to enlarge it.)

You ought to have the next outcome, with first and final names in separate columns. Shimon Brathwaite/IDG

The first and final names at the moment are in separate columns. (Click picture to enlarge it.)

Lastly, we have to spotlight each buyer with a steadiness of greater than zero. Highlight all the information within the third column (cells C:2 to C:8) after which click on Home > Conditional Formatting. Shimon Brathwaite/IDG

The ultimate step is to use conditional formatting guidelines to the information. (Click picture to enlarge it.)

Choose Highlight Cell Rules after which Greater Than. Enter 0 and click on OK to spotlight each buyer who has a steadiness larger than zero. Shimon Brathwaite/IDG

Formatting cells which can be larger than zero.

This needs to be the ultimate outcome: Shimon Brathwaite/IDG

All cells with a steadiness of larger than zero at the moment are highlighted in mild purple. (Click picture to enlarge it.)

Now that you’ve accomplished the duties sequence, return to the Developer tab and click on Stop Recording. Your first Excel macro is full. Shimon Brathwaite/IDG

When you’re accomplished recording your macro, hit Stop Recording. (Click picture to enlarge it.)

Important notes about working with macrosIf you need to run your macro once more, merely click on on the Macros button and it is going to be obtainable so that you can run. Or, should you assigned the macro a shortcut, merely press the important thing mixture to run it. Shimon Brathwaite/IDG

Click the Macros button at any time to see the checklist of macros obtainable for the workbook. (Click picture to enlarge it.)

Note that you just can’t save a spreadsheet with macros as a standard .xlsx workbook. You should put it aside as an Excel Macro-Enabled Workbook (.xlsm) to keep away from dropping your macros. Shimon Brathwaite/IDG

Save your workbook in .xlsm format to protect its macros. (Click picture to enlarge it.)

Once you’ve made that change, any time you need to work with a recent information set you’ll be able to merely reopen the workbook and import the information you need to work on by going to the Data tab and deciding on Get Data. You will be capable to import information from information, databases, and different on-line providers. Shimon Brathwaite/IDG

Save your workbook in .xlsm format to protect its macros. (Click picture to enlarge it.)

Be conscious that in lots of circumstances macros are disabled by default. That’s as a result of, as Microsoft notes, “VBA macros are a common way for malicious actors to gain access to deploy malware and ransomware.” To defend organizations from such threats, Microsoft now blocks macros in information from the web — and typically in these saved on the corporate’s shared drives. Your group might have imposed further restrictions on macros.So everytime you open an Excel workbook with macros in it (together with your personal), there’s a likelihood that you will note a warning message just like the one beneath. If your workbook is one that you just created or from a trusted supply, go forward and allow it. If the macro is from an untrusted supply, nonetheless, don’t allow it, as a result of it could be malware. Shimon Brathwaite/IDG

You would possibly see a warning if you open a workbook with macros in it.

Another vital characteristic to pay attention to when recording macros is the usage of relative references. This characteristic makes it in order that no matter the place the information begins on the spreadsheet, the macro will be capable to discover it and begin the processing there.For instance, the macro we created will all the time start processing at column A as a result of relative references weren’t toggled on. However, if we did the identical operations however clicked on Use Relative References first, then the macro would be capable to detect the place the data begins (Column C, for example) and start its processing from that time. This characteristic is beneficial if the information you’re working with is not going to all the time begin on the identical level. Shimon Brathwaite/IDG

Select Use Relative References in case your information received’t all the time begin on the identical level within the worksheet.

How to edit or create a macro with codeIf you need to see the VBA code behind a macro, go to the Developer tab, click on Macro, choose the macro, after which click on Edit.You might be taken to a pane the place you’ll be able to see the supply code for the macro you created. In the screenshot beneath, the underlined gadgets present actions that we carried out, akin to altering the names of the headings and deciding on rows. You can alter these for various use circumstances. For instance, if the information you’re employed with runs from vary A1:A20, chances are you’ll need to increase the vary to incorporate all doable cells. Shimon Brathwaite/IDG

Peeking on the code that underlies a macro. (Click picture to enlarge it.)

If need to attempt your hand at writing macros from scratch, there are a number of sources on-line for studying to write down VBA scripts, together with  Codewars, Udemy, and Codecademy. Back in Excel, click on Developer > Macros > Create. You’ll be taken to a clean pane the place you’ll be able to write VBA code.

Copyright © 2022 IDG Communications, Inc.