Microsoft Office Tutorials and References
In Depth Information
4.4 Run Time, Design Time, and Break Mode
Function FunctionName
in any code window (following the current End Sub or End Function statement, or in the
general declarations section). As soon as the Enter key is struck, Excel will move the line of code
to a new location and thereby create a new subroutine. (It will even add the appropriate ending—
End Sub or End Function .)
4.4 Run Time, Design Time, and Break Mode
The VBA IDE can be in any one of three modes: run mode, break mode, or design mode. When
the IDE is in design mode, we can write code or design a form.
Run mode occurs when a procedure is running. To run (or execute) a procedure, just place the
cursor anywhere within the procedure code and hit the F5 key (or select Run from the Run menu).
If a running procedure seems to be hanging, we can usually stop the procedure by hitting
CtrlBreak (hold down the Control key and hit the Break key).
Break mode is entered when a running procedure stops because of either an error in the code or a
deliberate act on our part (described a bit later). In particular, if an error occurs, Excel will stop
execution and display an error dialog box, an example of which is shown in Figure 4-2 .
Figure 4-2. An error message
Error dialog boxes offer a few options: end the procedure, get help (such as it may be) with the
problem, or enter break mode to debug the code. In the latter case, Excel will stop execution of the
procedure at the offending code and highlight that code in yellow. We will discuss the process of
debugging code a bit later.
Aside from encountering an error, there are several ways we can deliberately enter break mode for
debugging purposes:
Hit the Ctrl-Break key and choose Debug from the resulting dialog box.
Include a Stop statement in the code, which causes Excel to enter break mode.
Insert a breakpoint on an existing line of executable code. This is done by placing the
cursor on that line and hitting the F9 function key (or using the Toggle Breakpoint option
on the Debug menu). Excel will place a red dot in the left margin in front of that line and
will stop execution when it reaches the line. You may enter more than one breakpoint in a
procedure. This is generally preferred over using the Stop statement, because
breakpoints are automatically removed when we close down the Visual Basic Editor, so
we don't need to remember to remove them, as we do with Stop statements.
Set a watch statement that causes Excel to enter break mode if a certain condition
becomes true. We will discuss watch expressions a bit later.
 
 
Search JabSto ::




Custom Search