Excel on the Mac has not always been the same powerhouse it was on Windows. Macros really wouldn’t work unless they were created exclusively for the Mac.
Starting in 2013, Microsoft brought back macros. There are two types of macros: those you can create by quickly recording your actions, and those that use VBA to design more advanced automations. With Office 2016, Excel is using the same codebase on all platforms. This change will make it easier for macros to work across platforms.
So let’s take a look at how this currently works on macOS.
Enabling Macros in Excel on Mac
Working with macros in Excel on your Mac may not be enabled by default. This setting is because macros can be a possible malware vector. The easiest way to tell is to see if you have the Developer tab available on the Ribbon in Excel. If you don’t see it, it is simple to enable.
Click on Excel in the menu bar, and then select Preferences in the dropdown. In the menu, click on Ribbon & Toolbar. In the right-hand list, Developer should be at the bottom, click the checkbox. Finally, click Save and you should see the Developer tab show up at the end of the Ribbon.
After you create each workbook with macros, save it in a new format .xlsm to use the macros after reopening the file. If you forget, Excel will remind you each time you try to save. You’ll also need to enable macros every time you open the file.
Manually Recording a Macro in Excel on Mac
Though you can code macros, that might not be for everyone. If you’re not ready to start working with VBA, Excel lets you record the steps for your macro in an existing sheet. Click on the Developer tab to see your options.
You’re looking for the third option in the Ribbon, Record Macro. Click this, and a dialog pops up allowing you to name your macro and set a keyboard shortcut. You can scope your macro to the Current Workbook, a New Workbook, or in your Personal Macro Workbook. The Personal Macro Workbook is in your user profile and lets you use your macros between your files.
Once you record your actions, they are available on this same tab. Clicking macros will bring up the saved macros in your workbook. Click on your macro name and click Run to run your recorded actions.
Example 1: Daily Sales Total and Hourly Average
For an example macro, you are going to run through a daily sales sheet, with the sales broken down by hourly totals. Your macro is going to add a daily sales total, and then add an average in the last column of each hourly period. If you work in retail or other sales position, this is a helpful sheet to track revenue.
We need to set up the first sheet. Using this first blank as a template to copy into a new tab each day could save you some time. In the first column/row put Hour/Date. Across the top add Monday through Friday.
Then in the first column put a break down of hourly totals from 8-5. I used 24-hour time, but you can use AM/PM notation if you prefer. Your sheet should match the screenshot above.
Add a new tab, and copy your template into it. Then fill out your sales data for the day. (If you don’t have data to populate this sheet, you can enter =RandBetween(10,1000) in all the cells to create dummy data.) Next, click on Developer in the Ribbon.
Then, click on Record Macro. In the dialog enter the name as AverageandSum and leave it stored in This Workbook. You can set a shortcut key if you like. You can enter a description if you need more details on what the macro does. Click okay to start setting up the macro.
At the bottom of the hourly listings enter Daily Totals. In the cell next to it, enter =SUM(B2:B10). Then copy and paste that into the rest of the columns. Then in the header add Average after the last column. Then in the next cell down, enter =Average(B2:F2). Then, paste that into the cells in rest of the column.
Then click Stop Recording. Your macro is now able to use on each new sheet you add to your workbook. Once you have another sheet of data, go back to Developer and click Macros. Your macro should be highlighted, click run to add your sums and averages.
This example can save you a couple of steps, but for more complex actions that can add up. If you do the same operations on data with identical formatting, use recorded macros.
VBA Macros in Excel on Mac
Manually recorded macros in Excel help with data that is always in the same size and shape. It is also useful if you want to perform actions on the entire sheet. You can use your macro to prove the problem.
Add another hour and day to the sheet and run the macro. You’ll see the macro overwrites your new data. The way we get around this is using code to make the macro more dynamic using VBA, which is a slimmed down version of Visual Basic. The implementation focuses on automation for Office.
It is not as easy to pick up as Applescript, but Office’s automation is entirely built around Visual Basic. So once you work with it here, you are quickly able to turn around and use it in other Office apps. (It can also be a big help if you’re stuck with a Windows PC at work.)
When working with VBA in Excel, you have a separate Window. The screenshot above is our recorded macro as it appears in the code editor. The windowed mode can be helpful to play around with your code as you’re learning. When your macro gets hung up, there are debugging tools to look at the state of your variables and sheet data.
Office 2016 now comes with the full Visual Basic editor. It allows you to use the Object Browser and debugging tools that used to be limited to the Windows version. You can access the Object Browser by going to View > Object Browser or just press Shift + Command + B. You can then browse through all the classes, methods, and properties available. It was very helpful in constructing the code in the next section.
Example 2: Daily Sales Total and Hourly Average With Code
Before you start coding your macro, let’s start by adding a button to the template. This step makes it much easier for a novice user to access your macro. They can click a button to call the macro rather than digging into the tabs and menus.
Switch back to the blank template sheet you created in the last step. Click on Developer to get back to the tab. Once you’re on the tab, click on Button. Next, click somewhere in the sheet on the template to place the button. The macros menu comes up, name your macro and click New.
The Visual Basic Window will open up; you’ll see it listed as Module2 in the project browser. The code pane will have Sub AverageandSumButton() at the top and a few lines down End Sub. Your code needs to go between these two, as it is the beginning and the end of your macro.
Step 1: Declaring Variables
To begin, you will need to declare all of your variables. These are in the code block below, but a note about how they are constructed. You should declare all variables using Dim before the name, and then as with the datatype.
Sub AverageandSumButton() Dim RowPlaceHolder As Integer Dim ColumnPlaceHolder As Integer Dim StringHolder As String Dim AllCells As Range Dim TargetCells As Range Dim AverageTarget As Range Dim SumTarget As Range
Now that you have all of your variables, you need to use some of the range variables right away. Ranges are objects that hold sections of the worksheet as addresses. The variable All Cells will be set to all the active cells on the sheet, which includes the column and row labels. You get this by calling the ActiveSheet object and then it’s UsedRange property.
The problem is you don’t want the labels included in the average and sum data. Instead, you’ll use a subset of the AllCells range. This will be the TargetCells range. You manually declare its range. Its start address is going to be the cell at the second row in the second column of the range.
You call this by calling your AllCells range, using its Cells class to get that specific cell using (2,2). To get the final cell in the range, you will still call AllCells. This time using SpecialCells method to get the property xlCellTypeLastCell. You can see both of these in the code block below.
Set AllCells = ActiveSheet.UsedRange Set TargetCells = Range(AllCells.Cells(2, 2), AllCells.SpecialCells(xlCellTypeLastCell))
Step 2: For Each Loops
The next two sections of code are For Each loops. These loops go through an object to act on each subset of that object. In this case, you are doing two of them, one for each row and one for each column. Since they are almost exactly the same, only one of them is here; but both are in the code block. The details are virtually identical.
Before you start the loop for each row, you need to set the target column where the loop writes the average of each row. You use the ColumnPlaceHolder variable to set this target. You set it equal to the Count variable of the Cells class of AllCells. Add one to it to move it to the right of your data by appending +1.
Next, you are going to start the loop by using For Each. Then you want to create a variable for the subset, in this case, subRow. After the In, we set the main object we are parsing TargetCells. Append .Rows at the end to limit the loop to only each row, instead of every cell in the range.
Inside the loop, you use the ActiveSheet.Cells method to set a specific target on the sheet. The coordinates are set by using subRow.Row to get the row the loop is currently in. Then, you use ColumnPlaceHolder for the other coordinate.
You use this for all three steps. The first you append .value after the parentheses and set equal to WorksheetFunction.Average(subRow). This writes the formula for the average of the row into your target cell. The next line you append .Style and set that equal to “Currency”. This step matches the rest of your sheet. On the last line, you append .Font.Bold and set it equal to True. (Note there are not quotes around this one, as it is the boolean value.) This line bolds the font to make the summary info stand out from the rest of the sheet.
Both steps are in the code example below. The second loop swaps rows for columns and changes the formula to Sum. Using this method ties your calculations to the format of the current sheet. Otherwise, its linked to the size at the time you record the macro. So when you work more days or hours, the function grows with your data.
ColumnPlaceHolder = AllCells.Columns.Count + 1 For Each subRow In TargetCells.Rows ActiveSheet.Cells(subRow.Row, ColumnPlaceHolder).Value = WorksheetFunction.Average(subRow) ActiveSheet.Cells(subRow.Row, ColumnPlaceHolder).Style = "Currency" ActiveSheet.Cells(subRow.Row, ColumnPlaceHolder).Font.Bold = True Next subRow RowPlaceHolder = AllCells.Rows.Count + 1 For Each subColumn In TargetCells.Columns ActiveSheet.Cells(RowPlaceHolder, subColumn.Column).Value = WorksheetFunction.Sum(subColumn) ActiveSheet.Cells(RowPlaceHolder, subColumn.Column).Style = "Currency" ActiveSheet.Cells(RowPlaceHolder, subColumn.Column).Font.Bold = "True" Next subColumn
Step 3: Label Your Summaries
Next, label the new row and column, set RowPlaceHolder and ColumnPlaceHolder again. First, use AllCells.Row to get the first row in the range, and then AllCells.Column+1 to get the last column. Then you will use the same method as the loop to set the value to “Average Sales”. You’ll also use the same .Font.Bold property to bold your new label.
Then reverse it, setting your placeholders to the first column and last row to add “Total Sales”. You want to bold this as well.
Both steps are in the code block below. This is the end of the macro noted by End Sub. You should now have the entire macro, and be able to click the button to run it. You can paste all these code blocks in order into your excel sheet if you want to cheat, but where is the fun in that?
ColumnPlaceHolder = AllCells.Columns.Count + 1 RowPlaceHolder = AllCells.Row ActiveSheet.Cells(RowPlaceHolder, ColumnPlaceHolder).Value = "Average Sales" ActiveSheet.Cells(RowPlaceHolder, ColumnPlaceHolder).Font.Bold = True ColumnPlaceHolder = AllCells.Column RowPlaceHolder = AllCells.Rows.Count + 1 ActiveSheet.Cells(RowPlaceHolder, ColumnPlaceHolder).Value = "Total Sales" ActiveSheet.Cells(RowPlaceHolder, ColumnPlaceHolder).Font.Bold = True End Sub
What’s Next for Macros in Excel on Mac?
Recorded macros are great to use for predictable repetition. Even if it’s something as simple as resizing all cells and bolding headers, these can save you time.
Visual Basic opens the door for Mac Excel users to dig deep into Office automation. Visual Basic was traditionally only available on Windows. It allows your macros to adapt to the data dynamically, making them more versatile. If you have the patience, this can be the doorway to more advanced programming.