Looking at Macro Code

For many people, knowing how to record and play back a macro is sufficient for most of their needs. But what if you made a minor mistake while recording a macro? What if you recorded a complex macro that referenced a project by filename and then the filename was changed? Although you might not ever need to know how to write VBA code, much less create an entire macro with it, the first step to making simple changes or corrections is to understand how simple and logical the macro code can be.

For more information about the Visual Basic Editor, see "Using the Visual Basic Editor" on page 906.

If you were to start the Visual Basic Editor that is included as part of Microsoft Project and open the Friday_Report macro, this is the code you would see:

Sub Friday_Report()• ' Macro Weekly task report' Macro Recorded Tue 3/5/04 by Steve Masters.-V iewApply Name:="Tracking Ga&ntt" F ilterApply Name:="I&ncomplete Tasks" S ort Key1:="Finish", Ascending1:=True R eportPrint Name:="Slipping Tasks" End Sub

It's quite short and reasonably simple. You might already have made some guesses about what different sections of the code mean, such as information that also appears in the Microsoft Project interface. Table 27-2 gives descriptions of each line in the VBA code.

Table 27-2. Breakdown of Code in the Friday_Report Macro

Macro Code

What It Means

Sub Friday_Report()•

The beginning of the macro. Sub is short for subroutine,

which is what a macro really is. The text that follows is the

name of the macro.

' Macro Weekly task report

Any line that starts with an apostrophe is a comment and

' Macro Recorded Tue

is ignored by Visual Basic. You can use comments any

3/5/02 by Steve Masters.

where in a macro to remind yourself of what the different

parts do.

ViewApply Name:="Tracking

This line changes the view to the Tracking Gantt. The

Ga&ntt"

ampersand (&) comes before the letter that acts as an

access key on the View menu.

FilterApply Name:=

This line applies a filter to display only incomplete tasks.

"I&ncomplete Tasks"

Sort Key1:="Finish",

This line sorts the tasks by finish date in ascending order.

Ascending1:=True

ReportPrint Name:=

This line prints the Slipping Tasks report.

"Slipping Tasks"

End Sub

The end of the macro, like the period at the end of a

If, after recording the macro, you decide that you prefer to sort the tasks in descending order, it doesn't take that much time or trouble to record the macro all over again. But it takes even less time to simply edit the macro and change True to False in the line Sort Key1: = "Finish", Ascending1:=True.

Follow these steps to start the Visual Basic Editor: 1 Click Tools, Macro, Macros.

Tip Pressing Alt+F8 is another way to display the Macros dialog box.

2 In the Macro Name list, click the name of the macro you want to edit.

3 Click the Edit button.

The Visual Basic Editor starts and displays your macro code (see Figure 27-2). You can now begin editing the macro code.

Figure 27-2. Open VBE to review and edit your macro code.
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment