Home > Sample chapters > Microsoft Office > Excel

Control Visual Basic in Microsoft Office Excel 2007 Visual Basic for Applications

Key Points

  • Use an If structure to make a decision. Add an Else clause if you need different actions for True and False conditions.

  • When you use a MsgBox, use the Buttons argument to create explicit choices. When checking the result of a MsgBox, be sure to test against the proper constant.

  • When you use an InputBox, be sure to think through all the possible types of input—valid, invalid, non-existent. Create an If structure to handle all the possible conditions.

  • When you simply need to loop through a collection, a For Each is the easiest option.

  • When you need a counter to help you as you work through a loop—for example, to keep two objects synchronized—use a For loop.

  • When you need to loop for a while or until a condition is True, use a Do loop A Do loop is extremely flexible, but be careful that you don’t create an infinite loop.

  • Use permanent and temporary breakpoints when troubleshooting large macros or loops.