PDA

View Full Version : Solved: Debug.Print



YellowLabPro
04-15-2007, 07:43 PM
I am reading about this feature, Debug.Print
The one example is Debug.Print ActiveSheet.Name

A few initial questions:
1) The results are shown after the code is run in the Immediate window?
2) What other things can I use w/ Debug.Print? Are these objects, or properties or something else?
3) Is the immediate window only used w/ Debug.Print?

thanks,

YLP

Bob Phillips
04-16-2007, 02:32 AM
1) Yes, after that line of code is ru, that is it outputs imemdiately.

2) Debug.Print can output anything that resolves to a numeric or string expression, such a Range("A1").Value, or myVar, or "abc".

3) No. You can set variable in the immediate window (myvar=17), you can output directly in the immediate window (?myVar), and so on. If single steeping through code, the immediate window can be very useful.

YellowLabPro
04-16-2007, 04:16 AM
Hi xld,
Thanks. I see the usefulness of the Immediate Window. To become better acquainted I search the web for examples. If you know of any, would you mind posting?
On a same sort of topic, I have yet to figure out the Watch Window. There have been mentions, but I have not seen anything there.

edited:
I found something right after I posted:
http://www.cpearson.com/excel/Debug.htm

thanks,

ylp

Bob Phillips
04-16-2007, 04:38 AM
The Watch window is like the Local window. You can add an item to the Watch window (Debug>Watch), but the advantage here is that you can watch an object, such as Worksheets("Sheet1"), and the Watch window contains all of the object, so you can drill down to its properties, such as Name. A simpler way to add to the Watch Window is to just drag an object or a vraibale into the window.

Bob Phillips
04-16-2007, 04:45 AM
Doug,

Here is an extract from a presentation I do on VBA. Although it is best presented not read, you might find it helpful.

2.5 Debugging
A number of debugging aids are provided in the VBIDE, many of which are described here.

2.5.1 Stepping Through Code
When testing code, it is useful to be able step through it one line at a time. It is then possible to watch the program flow, and check various variables or objects at any point. This helps to determine whether the code is executing as expected, and helps track down the cause if it is not.
The F8 key allows single stepping through the code. Some way of initiating the stepping is required, as normally the code runs from start to finish (or errors) without any opportunity to intervene. The initial procedure can be initiated in single step mode by placing the cursor anywhere within that procedure and hit F8 or Debug>Step Into from the menu. Another way is to add a Break Point somewhere within the code (see below), and then start single stepping when the code breaks by hitting F8, or Debug>Step Into.
If another procedure is called from a procedure when single stepping, VBA steps inside that procedure and continues as before. At the end of the ‘called’ procedure, VBA continues the stepping in the ‘caller’ procedure.
A line of code, or lines of code, can be stepped-over by using Shift-F8, or Debug>Step Over from the menu. When stepped over, the code is still executed, but immediately. This can be useful if another procedure is called within your code, an d you do not want to single step the whole procedure.
The final stepping function is step out, invoked by Ctrl-Shift-F8, or Debug>Step Out on the menu. CTRL+SHIFT+F8 to "Step Out" of the current procedure. In this case, VBA executes until the end of that procedure, stopping at the line of code immediately following the call to that procedure (in the ‘caller’ procedure), or the end of the program if there is no caller.

2.5.2 Immediate Window
The Immediate Window is a window in the VBIDE in which you can enter commands and view and change the contents of variables. These commands can be entered while the code is in Break mode (see below), or when no code is executing.
To show the Immediate Window, press Ctrl-G, or View>Immediate Window from the menu.
Variables can be evaluated in the Immediate Window by using the ? command, by typing ? followed by the variable. For example,
?ActiveCell.Address
$A$1
It is also possible to change the value of variables in the Immediate Window, or set application values. In these cases, the ? operator is omitted, simply type
myVar = 17
to set a variable, or
Application.EnableEvents=True
to reset events. This can be very useful if an unexpected situation is encountered, or to check a ‘what-if’ situation.
It is not possible to run several lines of VBA code in the Immediate Window, but it is possible to con-join several statements using the ':' character. For example, to display each element of the array variable Arr use the following in the Immediate Window.
For Each cell In Range("A1:A10"): MsgBox cell.Address: Next cell
There is facility built into the VBE to clear the Immediate Window, but the MZTools addin has such a function (see ‘Useful Tools’ below).

2.5.3 Debug.Print
The Debug.Print statement can be embedded in the code to display messages or details of variable. The Debug.Print statement outputs to the immediate window.
The statement
Debug.Print "Value of myVar is" & myVar
Is the equivalent of
? "Value of myVar is" & myVar
entered directly into the Immediate Window.

2.5.4 Debug.Assert
In Excel 2000 and later, you can use Debug.Assert statements to cause the code to break if a condition is not met. The syntax for Debug.Assert is:
Debug.Assert (condition)
where condition expression that returns True (-1) or False (0). If the condition evaluates to False or 0, VBA breaks on that line (see, below).
An example usage could be within a loop that has an exit within it to test whether the loop has completed successfully

Both Debug.Print and Debug.Assert are development aids, and as such they should be removed before production release. In the case of Debug.Print. not too much damage may be caused by not removing them, but the Debug.Assert could cause a code interruption, and so it is imperative to remove them.

2.5.5 Break Points
A break point is a setting on a line of code that causes VBA to pause execution immediately before that line of code is executed.
Break points can only apply to code lines, not to comment lines, or declarations (general, or within a module).

To put a break point on a line of code, place the cursor on that line and press F9; Debug>Toggle Breakpoint from the menu; or click in the left-hand bar alongside the line of code.
To remove a break point, repeat the action.

When a line contains a break point, it is displayed with a brick colour background. Immediately before this line of code is executed, background becomes yellow.

The code breaks before the line of code is executed. When the code breaks, it is possible to check things in the immediate window, drag a variable to the watch window, check the call stack, etc.
Code execution is resumed after a break by pressing F5; Run>Continue from the menu; or stepping through the code line by line.
When the file is closed, all break points are removed.

2.5.6 Run To Cursor
Run To Cursor tells VBA to execute code until the line on which the cursor is placed is reached. When execution gets to this line, VBA enters break mode.
Invoked with Ctrl-F8, or Debug>Run To Cursor.

Once the code breaks, it is similar to a normal Break Point, for instance it can be resumed in the same way. One difference is that the break point is temporary. The second time that line of code is executed, code execution does not pause.

2.5.7 Watch Window
The Watch Window allows you to "watch" a specific variable or expression and cause code execution to pause and enter break mode when the value of that variable or expression is True (non-zero) or whenever that variable is changed. (Note, this is not to be confused with the Watch object and the Watches collection).
To display the Watch Window, choose it from the View menu. To create a new watch, choose Add Watch from the Debug menu. This will display the Add Watch window, shown below.

There are three types of watches, shown in the Watch Type group box. "Watch Expression" causes that watch to work much like the Locals Window display. It simply displays the value of a variable or expression as the code is executed. "Break When Value Is True" causes VBA to enter break mode when the watch variable or expression is True (not equal to zero). "Break When Value Changes" causes VBA to enter break mode when the value of the variable or expression changes value.

You can have many watches active in your project, and all watches are displayed in the Watch Window. This makes is simple to determine when a variable changes value.

YellowLabPro
04-16-2007, 04:55 AM
Thanks Bob for this great post, I will keep it nearby as I go through the methods.
I get most of the concepts here, the Watch Window is still hazy. I am not requesting this from you, unless of course you have some private stash... :-), but what would be nice for us new guys would be some sort of video demo-- to show use and purpose of the less used features, just to tie things together.