Stepping Over a Procedure

Suppose the current statement in MyProcedure calls the SpecialMsg procedure. If you choose Debug | Step Over (Shift+F8) instead of Debug | Step Into (F8), Visual Basic will quickly execute all the statements inside the SpecialMsg procedure and select the next statement in the calling procedure MyProcedure. During the execution of the SpecialMsg procedure, Visual Basic continues to display the Code window with the current procedure.

1. Enter the following procedure in the current module:

Sub MyProcedure() Dim myName As String Workbooks.Add myName = ActiveWorkbook.Name ' choose Step Over to avoid stepping through the ' lines of code in the called procedure - SpecialMsg SpecialMsg myName Workbooks(myName).Close End Sub

Sub SpecialMsg(n As String) If n = "Book2" Then

MsgBox "You must change the name." End If End Sub

2. Add a breakpoint at the following statement: SpecialMsg myName

3. Place the insertion point anywhere within the code of MyProcedure, and press F5 to run it. Visual Basic halts execution when it reaches the breakpoint.

4. Press Shift+F8, or choose Debug | Step Over. Visual Basic quickly runs the SpecialMsg procedure and execution and then advances to the statement immediately after the call to the SpecialMsg procedure.

5. Press F5 to finish running the procedure without stepping through its code.

Stepping over a procedure is particularly useful when you don't want to analyze individual statements inside the called procedure (SpecialMsg).

Another command on the Debug menu, Step Out (Ctrl+Shift+F8), is used when you step into a procedure and then decide that you don't want to step all the way through it. When you choose this option, Visual Basic will execute the remaining statements in this procedure in one step and proceed to activate the next statement in the calling procedure.

In the process of stepping through a procedure, you can switch between Step Into, Step Over, and Step Out options. The option you select depends on which code fragment you wish to analyze at a given moment.

The Debug menu Run to Cursor (Ctrl+F8) command lets you run your procedure until the line you have selected is encountered. This command is really useful if you want to stop the execution before a large loop or you intend to step over a called procedure.

Suppose you want to execute the MyProcedure procedure to the line that calls the SpecialMsg procedure.

1. Click inside the statement SpecialMsg myName.

2. Choose Debug | Run to Cursor. Visual Basic will stop the procedure when it reaches the specified line.

3. Press Shift+F8 to step over the SpecialMsg procedure.

4. Press F5 to execute the rest of the procedure without single stepping.

0 0

Post a comment