Testing Procedures Using the Immediate Pane

In the Immediate pane, you can evaluate any valid Visual Basic executable expression or statement, including calls to Sub and Function procedures. You can evaluate an expression or Function procedure by printing the value it returns in the Immediate pane. You can also test the possible effect of a Sub procedure with any given set of arguments by entering it as a statement in the Immediate pane just as you would in a module.

From a module, you can print a value to the Immediate pane by using the Print method of the Debug object (the Debug object refers to the Immediate pane). The following example prints the name of the active workbook in the Immediate pane.


Sub DebugExample()
    Debug.Print ActiveWorkbook.Name
End Sub

Note

The Immediate pane doesn't open automatically when Visual Basic encounters Debug.Print. If you don't have the Immediate pane open, you won't see the values displayed by Debug.Print.

From the Immediate pane, you can use the Print method without the Debug object qualifier. You can also use a question mark (?) as shorthand for the Print method. When you type the following code in the Immediate pane and then press ENTER, Visual Basic displays the same result as it does when you run the preceding code example from a module.


? ActiveWorkbook.Name

You can test a Sub procedure by typing it and its arguments in the Immediate pane, as in the following example.


SizeIt 5000, 3000

Note

You cannot use the Print method or a question mark in the preceding example because a Sub procedure doesn't return a value to display.

You can execute any built-in function or statement in the Immediate pane. However, a control structure is valid only if it can be completely expressed on one line. For example, the following For Each...Next loop can be executed in the Immediate pane.


For Each s In Sheets : Print s.Name : Next s