How to automate your spreadsheets with Excel macros

I want to teach you with this article on how to join multiple tasks into a single one-second event and this way automate a lot of mundane time-consuming task in your excel workflow. .

Excel is a fast working application for executing various calculations, accounting, analysis, visualization, etc. In fact, it takes one-tenth of a second to calculate a large spreadsheet, nevertheless, it's the manual work of typing and formatting that slows you down your workflow. That is the reason you want Excel macros to connect multiple tasks into a single action.

In this article you will learn:

I have given a sample workbook for the for the scenarios described in this article so you can follow along step by step. If needed, you can create a custom spreadsheet with your values in it.

Getting started

Let's begin with our first macro. Once you understand how easy it is to automate tasks using macros, you’ll never look back. But, before we jump in, here are some tips on how to manage your data before implementing a macro function:

Why are small macros important

For this example, we have an eCommerce store owner who has expanded his territory from a single store to 8 different major cities. Now the CEO has been running his own books for years, which was a challenging task for a single store, and now he has 8. He has to collect data from each store and merge it to monitor the progress of the entire company.

We built a few smaller macros to accomplish the following tasks:

Once small macros are built and tested, we can merge them into one master macro or just leave them as independent smaller macros. Either way, it is advised to keep smaller macros because it's much easier to work and debug them in contrast to big and detailed macros.

Preparing the Master spreadsheet

The first thing to remember is that In case you are building a spreadsheet from scratch, always start building your master spreadsheet first.

In the following chapter, I will guide you through a step by step process of creating a master spreadsheet:

  1. Row 1: Enter the date formula in A1 and the store location in B1. See image below:
  2. A1: =Today()

    B1: Branch name and number



  3. Row 2: Once the dynamic date and static info are entered, leave the entire second row reserved for the necessary calculations of every column. This is a good practice because this row is fixed and always visible.


  4. Row 3: Enter the category names and any other field-specific information you would need. In the example below, they are extended from A3 through J3


  5. Column A: Enter the static data in column A. (values in your spreadsheet that rarely change). If your project works with product numbers or ID codes (in our example SKU numbers) or other unique value per product, enter them in column A beginning from row 4 (don’t skip to row 5). Other static data fields might include the Product Description, the Product Price, sales tax percentage, etc. - these values can be placed in separate columns.


  6. Every row in column A must include a unique value for two main reasons:

    • Column A is the main navigational column for our macro. It navigates through the spreadsheet based on the Home (A1) position and column A. (If some of the cells contain a blank field, it will disrupt the macro function)
    • If you want to build multiple tables later and connect them through Pivot Reports, you must have unique values from each table to properly join them.

  7. There is a usual standard location for each category but this depends on your type of data.
    • Column B = Product Description
    • Column C = Quantity sold
    • Column D = Product price
    • Column E = Extended Cost
    • Column F = Price Discounts
    • Column G = Sales Tax
    • Column H = Totals

    Remember, the total values of these fields are in the static row 2 for each column.

  8. Once the master database structure is set up, do not change anything. If there's a need of adding additional columns to the table, use the insert column command.
    For example, if you want to add a second discount level, position your cursor anywhere on column G and click the tab: Home > Insert > Insert Sheet Columns. This process does not affect the macro function.

  9. The same rule applies to rows. Be careful when inserting rows inside an active. For instance, if the formula in row 2 states =SUM(B3:B20) and you insert a row outside of the formula’s range like B21, the new record won't be included in the formula's calculation and therefore you have to modify it to =SUM(B3:B21)

  10. Let's set up the formula range to include all the current and possible future rows, this way you won't need to change this in the future.
    • C2: =SUM(C4:C500)
    • E2: =SUM(E4:E500)
    • F2: =SUM(F4:F500)
    • G2: =SUM(G4:G500)
    • H2: =SUM(H4:H500)



    As seen above, we set the formula range to be several rows beyond our last record (500), so if there's ever a need to add new records, we can do that easily and not worry about adjusting the range.

  11. At this stage, let's begin entering the formulas to calculate the different values in these columns:
    • E4: =SUM(C4 * D4), then copy from E4 down to E5:E500
    • F4: =SUM(E4 * 10%), where 10% represents the current discount percentage, then copy from F4 down to F5:E500
    • G4: =SUM(E4-F4) * 6.25%, where 6.25% represents the current Sales Tax in your country, then copy from G4 down to G5:G500
    • H4: =SUM(E4-F4+G4), then copy from H4 down to H5:E500



  12. After all the spreadsheet formulas are inserted, all you will have to do is enter the sold quantity for each product in column C (daily, weekly, or monthly). If the product prices change, enter the new figures in column D. The foundation of this database is all formulas and static information.

  13. Once we established our spreadsheet with a proper structure, our fixed data, and correct formulas, make 8 copies in worksheets 2 through 9. Rename every worksheet at the bottom to identify every store and change the name of the sheet1 work to 'Master'.

  14. 6-5

  15. Change the location data on row 1 to identify the store information (that matches the store on the tab) on all 9 spreadsheets. In this step you can email a copy of each branch spreadsheet to each of the store managers; for example, send the Texas sheet to Texas, etc. Their copies include the spreadsheet with static data of their branch and formulas that operate on their spreadsheet only.

  16. The macro function will provide the formulas (grand totals of all stores) for the Master spreadsheet only.

  17. Once the individual stores get back to you with their spreadsheets, you will copy the individual sheets from the 8 stores manually to your master spreadsheet.

Automate data collection

Program Macro1

  1. Access your dedicated database folder on your PC and open your MasterDB.xlsx spreadsheet (example: C:\Users\Exgap\Documents\Database\masterDB.xlsm)

  2. Open one of the new store spreadsheets (example: (example: C:\Users\Exgap\Documents\Database\MiamiDB.xlsm)

  3. Move your mouse cursor back to the MasterDB.xlsx to be the active sheet.

  4. Select the View tab>Macros>Record Macro

  5. 6-5

  6. You can set the macro name field as "MiamiDB_Macro".

  7. Enter a shortcut key in the Shortcut_key field box (Optional)

  8. In the "Store Macro", select the dropdown arrow and select Personal Macro Workbook from the list, then click OK.

  9. 6-5

    Done, at this point, you are recording every step you make in the macro. Follow the next directions, and use your mouse to navigate through the spreadsheet.

Execute Macro1

  1. Move back to the MiamiDB spreadsheet, right-click the Miami worksheet tab and in the popup menu, select "Move or Copy"

  2. In the "Move or Copy" dialog box, check the "Copy" box.

  3. In the "Move Selected Sheets" dialog box, select the "To Book" dropdown arrow and choose "MasterDB.xlsx"

  4. In the "Before the Sheet" box, select the first spreadsheet on the list which in our case it's the “Master” spreadsheet then click OK. That lets Excel copy the sheet to the MasterDB under a new worksheet called “Miami2.”

  5. 6-5

  6. Right-click the tab of the original Miami spreadsheet and pick Delete from the popup menu.

  7. In order to truly replace the new Miami sheet that the Miami manager sent to you, rename the new sheet by deleting "2" (Miami2 > Miami)

  8. Position the new Miami sheet where the original sheet was located ( between Las Vegas and New Orleans tabs)

  9. Go to View tab > Macro > Stop Recording

  10. Save the MasterDB.xlsm and MiamiDB.xlsm files

  11. Go back to MasterDB and run the macro with the custom shortcut or go to View tab > Macro > View Macros > Run

  12. 6-5


  13. If the macro function works as we wanted, repeat the process for the rest of the 7 spreadsheets.

Automate data collection

Programming Macro2

  1. Access your dedicated database folder on your PC and open your MasterDB.xlsx spreadsheet (example: C:\Users\Exgap\Documents\Database\masterDB.xlsm)

  2. Select the View tab>Macros>Record Macro.

  3. You can set the macro name field as "Sorting_Macro".

  4. Enter a shortcut key in the Shortcut_key field box (Optional)

  5. In the Store Macro, select the dropdown arrow and select "Personal Macro Workbook" from the list, then click OK.

Executing Macro2

  1. Select a worksheet from the MasterDB

  2. Press Ctrl+ Home in order to move the cursor to A1 cell.

  3. Press the Down arrow key three times.

  4. Press Shift+Ctrl+Down + Right in order to highlight the table content

  5. Navigate to Data > Sort and from the Column dropdown menu choose "SKU Number", from the Sort on dropdown menu pick "Cell Values" and from the Order dropdown menu choose "A to Z". Press OK

  6. 6-5

  7. Press Ctrl+ Home again

  8. Repeat the process for the next 8 spreadsheets

  9. Once you finished the process, pick the Master spreadsheet tab and press Ctrl+ Home.

  10. Stop Recording the macro by navigating with your courser to View > Macro > Stop Recording

  11. Save the MasterDB. (Ctrl+S)

  12. At this point, you can go ahead and test out the macro function by using the custom shortcut OR by going to View tab > Macro > View Macro > choose "PERSONAL.XLSB!Sorting_Macro" and press "Run"

Automate formulas

Programming Macro3

Formulas for individual stores are already in position, we entered them back in the Prep Work section above. The following formulas will be for the Master Spreadsheet alone which will calculate the combined sum of all store spreadsheets.

The programming part of this macro is going to be the same as for Macro1 and Macro2, and it follows:

  1. Access your dedicated database folder on your PC and open your MasterDB.xlsx spreadsheet (example: C:\Users\Exgap\Documents\Database\masterDB.xlsx)

  2. Select the View tab>Macros>Record Macro.

  3. You can set the macro name field as "Formula_Macro".

  4. Enter a shortcut key in the Shortcut_key field box (Optional)

  5. In the Store Macro, select the dropdown arrow and select "Personal Macro Workbook" from the list, then click OK.

Executing Macro3

  1. Pick the Master spreadsheet tab

  2. Press Ctrl+Home

  3. Press Down-Right-Right

  4. =SUM(Philadelphia:Seattle!C2) and press Enter key (This formula has to be located in the Cell C2, while the tabs name Philadelphia and Seattle represents the first and last spreadsheet tab in the MasterDB workbook excluding the Master Workbook which has to positioned before Philadelphia)

  5. 6-5

  6. Press Right-Right-Shift+Right-Right to highlight cell values and hit Enter. (This way Excel will enter the rest of the formulas for you from E2 to H2 cells)

  7. While the cells are highlighted, press Shift+Ctrl+4 in order to format the cells as Currency values.

  8. You can go ahead and test out the macro function by using the custom shortcut OR by going to View tab > Macro > View Macro > choose "PERSONAL.XLSB!Formula_Macro" and press "Run"



ABOUT

    "Our main focus is on helping people to overcome barriers into Data Analysis and Visualization with help of MS Excel, Power BI and Tableau platform in organizations and in an online education environment with help of ExGap Blog and ExGap Course"

    Continue reading here

RECENT ARTICLE

SIGN UP

Stay up to date on the most popular data analysis and visualization products out there, and learn how to make data-driven decisions. All of our courses ensure quality, usability, and uniqueness of the content.

By creating an account you agree to our Terms & Privacy.