• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 28. Developing Visual Basic Macr... > Writing VBA Code That Will Work Next...

Writing VBA Code That Will Work Next Month

It was suggested that the best code to write is code that will keep on working long after you originally wrote it. You have already seen a number of techniques that make this happen. For example, code that loops from task ID 1 to 100 in a project with 100 tasks will have problems if more tasks are added—the extra tasks will be missed. The following techniques all support code survival:

  • For Each T in ActiveProject.Tasks guarantees that every task will be processed. Alternatively use the ActiveProject.Tasks.Count property to make sure that every task is processed.

  • Testing for empty tasks—even if your plan doesn't have them—is also good, because you never know when an empty task might be added (see the TaskSummaryName macro discussed previously in this chapter).

  • Not having fixed dates is another good survival technique. Wherever possible, use the Date function to return the current day's date and then adjust the date as needed. For example, Date + 14 gave you the date two weeks into the future (refer to the FilterNextTwoWeeks macro in Chapter 27).

  • Outputting reports to Excel means that one-off changes in requirements can be handled manually in Excel without needing macro changes (take a look at the ProfitLossReport macro).

  • Applying specific views and tables can help, because if the user wants a slightly different view, they need only reformat the view for the macro to keep working (as discussed in Chapter 27). If all formatting is in the macro, any format changes will require macro edits.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint