Doing the Impossible
In 2010, I attended a Baltimore MPUG meeting that changed my life. I learned about Visual Basic for Applications (VBA) for Microsoft Project, and I was stunned to discover easily-performed operations that I had previously thought were impossible.
What is VBA?
Visual Basic for Applications, or VBA, is behind the scenes in all of the Microsoft Office applications. When you expose the “Developer” tab you will see it (shown in Figure 1 below).
VBA is an object-oriented language, which means that an “object” has “properties” and “methods.” An example object is “Task,” and for that an example property is “Name” and an example method is “Delete.” A very respectable way to discover the VBA instruction for any action is to record a macro. Here is the macro recording of adding one holiday to a holiday calendar (Figure 2).
The space-underscore at the end of one line allows the instruction to continue on the next line. To learn about available objects and their associated properties and methods, see the Object Model via the Help tab (Figure 3).
Here’s an example of doing the impossible! Suppose that you need to create a text file containing a task’s Notes and Successors (Figure 4). You could open the Gantt Chart, copy each field, and paste the data into the text file, but you’ll be limited because only the first 255 characters can be copied (Figure 5). In contrast, the VBA instructions in Figure 6 will produce the full strings as shown in Figure 7.
As you can see using VBA has its advantages. Here are some personal examples from the last decade:
- Searching for redundant logic in MS Project, and then deleting each redundancy.
- Exporting monthly resource-loading into an Excel spreadsheet in the exact format required to upload that into an enterprise Resource Planning System.
- Building a summary schedule for populating a spacecraft circuit board, based upon a detailed Excel work-flow report from an enterprise Manufacturing Planning System.
- Finding the months spanned by tasks in each WBS element.
- Tracing the driving predecessors path to any selected task.
- Tracing the driven successors path from any selected task.
I recently developed US and Canadian holiday calendars for the years 2020-2050, which yield much more realistic schedules than assuming every weekday is a workday. I’ll demonstrate below how I used VBA to automate this production.
Automating the Process to Generate a Holiday Calendar
Using VBA, I determined the actual dates for all of the fixed-day holidays, such as the Canadian “Family Day” on the third Monday in February. Here is the VBA code to find the third Monday in February (Figure 8). The results were written into comma-separated-values file for this one and all the other fixed-day holidays.
The fixed-date holidays (for example, the US’ Independence Day on July 4) were handled manually in an Excel spreadsheet, making adjustments for those instances where the dates fell over a weekend.
Once I had determined all the holidays, I populated the list within MS Project. Doing this manually is error-prone and time-consuming, so I used the VBA instruction discovered in Figure 2 along with Excel formulas to construct a sequence of 350 VBA instructions. See Figure 9.
At this point, I simply copied the VBA instructions from the Excel spreadsheet, pasted them into an empty VBA module, and executed them. You can see the results here.
Going Forward with Your Own Use of VBA
Getting started is hard. In the below resources list, I have posted a VBA structural template with all of the necessary sections, along with some hard-to-discover instructions and a few consultants I recommend. I’ve also included a collection of VBA code snippets for the most-likely requirements.
#1. VBA template and code snippets available via my Google Drive.
#2. Book: VBA for MS Project by Rod Gill. Out of print, but a PDF is available via MSProjectHolidayCalendars.com. A few used physical books remain at Amazon.com.
#3. Consultant: Rod Gill via firstname.lastname@example.org and at Microsoft Project VBA development services (project-systems.co.nz) and also at Introduction to Project VBA – how to boost your productivity – YouTube
#4. Consultant: Minerva Goree | Microsoft Project Consultant and Teacher | Udemy | email@example.com
#5. Consulting Company: Sarah Howard via firstname.lastname@example.org
#6 Microsoft official reference at Project Visual Basic for Applications (VBA) reference | Microsoft Docs
What have you done in MS Project with VBA? I’d love to hear from you in the comments.