Using the Immediate Window

     

The Watch window tells you the current value of an expression, but you'll often need more information than this. You also might want to plug in different values for an expression while in break mode. You can perform these tasks with VBA's Immediate window, which you display by activating the View, Immediate Window command.

Printing Data in the Immediate Window

You can use the Print method of the special Debug object to print text and expression values in the Immediate window. There are two ways to do this:

  • By running the Print method from the procedure

  • By entering the Print method directly into the Immediate window

The Print method uses the following syntax:

 Debug.Print  OutputList  

OutputList

An expression or list of expressions to print in the Immediate window. If you omit OutputList , a blank line is printed.

Here are a few notes to keep in mind when using this method:

  • Use Spc( n ) in OutputList to print n space characters .

  • Use Tab( n ) in OutputList to print n tab characters.

  • Separate multiple expressions with either a space or a semicolon.

Running the Print Method from a Procedure

If you know that a variable or expression changes at a certain place in your code, enter a Debug.Print statement at that spot. When you enter break mode, the OutputList expressions appear in the Immediate window. For example, Figure 15.12 shows a procedure in break mode. The information displayed in the Immediate window was generated by the following statement:

 Debug.Print "The backup filename is "; backupName 
Figure 15.12. Use Debug.Print in your code to display information in the Immediate window.

graphics/15fig12.jpg

Running the Print Method in the Immediate Window

You can also use the Print method directly in the Immediate window to display information. Note that when you're in break mode, you don't need to specify the Debug object.

Figure 15.13 shows a couple of examples. In the first line, I typed print backupdrive and pressed Enter. VBA responded with A:. In the second example, I typed ? backupname (? is the short form of the Print method), and VBA responded with A:Chaptr15.xls.

Figure 15.13. You can enter Print statements directly in the Immediate window. Note the use of the question mark (?) as a short form of the Print method.

graphics/15fig13.jpg

Executing Statements in the Immediate Window

Perhaps the most effective use of the Immediate window, however, is to execute statements. There are many uses for this feature:

  • To try some experimental statements to see their effects on the procedure.

  • To change the value of a variable or property. For example, if you see that a variable with a value of zero is about to be used as a divisor, you could change that variable to a nonzero value to avoid crashing the procedure.

  • To run other procedures or user -defined functions to see if they operate properly under the current conditions.

tip

graphics/tip_icon.gif

You can execute multiple statements in the Immediate window by separating each statement with a colon . For example, you can test a For...Next loop by entering a statement similar to the following:

 For i=1 To 10:Print i^2:Next 

You enter statements in the Immediate window just as you do in the module itself. For example, entering the following statement in the Immediate window changes the value of the backupName variable:

 backupName = "B:Chaptr15.xls" 


Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net