Automate the Boring (and Persnickety) Stuff with VBA

By Mary Pat Campbell

Mary Pat headshot

Many years ago, I had “ownership” of a monster spreadsheet that had been used for preparing exhibits for an insurer’s Annual Statement. This spreadsheet had grown over the years, with each additional owner adding their own touch to try to make sure the results were correct. However, no one ever had enough time to really do a thorough clean up job.

Somewhere along the way, pivot tables became the heart of these spreadsheets. Pivot tables were used to summarize the results from hundreds of rows, with rows being added and updated throughout the process. You need to know only one fact about pivot tables: while they are a quick and flexible tool to summarize, aggregate, and otherwise slice-and-dice data, they do not automatically update or “refresh” when the underlying data is changed.



Join Mary Pat on September 10th for her latest webinar: Automating with VBA in Excel

At least ten pivot tables lived in this particular file, spread out over even-more-numerous sheets within the workbook. Invariably, as data was updated and appended, the pivot tables being used for aggregation would get out-of-sync with that data. There were counter-checks to results (primarily: do the reserves add up to the same number for every different exhibit?), but time was wasted from the various counterchecks and reminders to manually update things.

I knew there had to be a better way.

I wrote VBA (Visual Basic for Applications) code to refresh all the pivot tables in the workbook:

VBA 1

Now, Microsoft did update Excel such that one can do as the above with a single button click (needless to say, I wrote the above code before that feature was added). However, the “Refresh All” button really does refresh everything – links to external files and data sources, in addition to refreshing pivot tables. However, it does simplify the code:

VBA 2

However, if one wanted to refresh only the pivot tables, my first set of code is what you would want to use.

One need not know a great deal of VBA (nor of computer science) in order to get usefulness out of VBA. Automating small steps that one has done manually can not only save yourself a lot of time (and boredom), but it can ensure repetitive tasks are done correctly.

One of the most notorious sources of spreadsheet error is the transfer of results from one spreadsheet to another location (the infamous “copy/paste” errors). When this sort of transfer is done manually, this can lead to items being pasted to the wrong location. Being off by one row or one column can be the source of errors that then propagate throughout one’s work. In the European Spreadsheet Risk Interest Group collection of horror stories, manual processes have led to large losses… and that’s just the ones that have made the news. Most of these manual processes could be replaced with an automated one, precluding these errors.

In my upcoming webinar on September 10, I will be covering how to use VBA to do some automation, including how to take results from the built-in macro recorder to make it repeatable for useful code.

Automate the boring and nit-picky things away with VBA – you’ll be glad you did!