|
Stop Statement
Stop
Insert Stop in the code. The statement is saved with the file, and so is in effect when the file is re-opened.
Breakpoint
There are three ways to set a breakpoint, two from toolbars and one direct.
The hand is the breakpoint icon in the Debug and Edit toolbars. Clicking in the left margin of the code pane can also set or reset the breakpoint. Breakpoint can’t be set on a comment or a blank line.
If the breakpoint is set, the line is brown, and there is a brown circle in the left margin.
If execution has stopped on a breakpoint, the yellow arrow will be on top of the brown circle. Space at the beginning of the line will be brown, and the rest of the line will be yellow.
Breakpoints are not saved with the file, and so have to be set again when the file is re-opened.
MsgBox
MsgBox "Just before failure point"
MsgBox is not often used in debug. It may be a quick way to look at one point, but often many points are needed, or the point is in a loop. The need to manually intervene on each occurrence will soon show this to be a very cumbersome way to debug. If MsgBox is just being used to stop the code, use Stop statements or breakpoints instead. If it is being used to display a value, use Debug.Print to show the value in the Immediate Window.
Debug Object
The Debug Object is a very simple object with no properties and only two methods, Assert and Print.
Debug.Assert stops when the expression evaluates to False
Debug.Assert expression
It performs the same as
If expression = False Then Stop
Debug.Print prints the list of text and variables following the statement. The semicolon list separator (;) concatenates the fields it separates, separated by a space if both fields are numeric. The comma list separator (,) starts the second field at a multiple of 14 spaces. The output of Debug.Print appears in the Immediate Window.
A Debug.Print statement ending in comma or semicolon does not finish the output line.
Sub temp1()
Dim x%
For x% = 1 To 5
Debug.Print x%; ' do not complete the output line
Next x%
Debug.Print ' complete the output line
End Sub
Log File
See sheet Lesson 7 in UserForms Lesson 7 2007.xlsx. These procedures are already in Module UserFormLessons6thru10. Be careful when logging from inside loops. A very large file can be created very quickly.
Sub TestLogFile()
Dim fName$, x%
chgDirIfNeeded ' logfile goes in this folder
fName$ = "Lesson7 Log_File.txt"
For x% = 1 To 10
LogTransactionToLogFile fName$, x%, x% + 10, x% * x%
Next x%
End Sub
Sub LogTransactionToLogFile(fName$, n1, n2, n3)
Close #1 ' just to be sure it's closed
Open fName$ For Append As #1 ' will create new file if needed
Print #1, "Logfile Demo", "x%=" & n1, n2, n3
Close #1
End Sub
Watch Window
The Watch Window is for detecting when a cell changes to a target value. Since the cells of interest to a UserForm are linked to controls by the ControlSource property, their values are also available to the code by the Value property. Therefore Watch Window won’t be covered in this lesson.
Notes
Which Debug Method to Use?
Breakpoint is the quickest to use, but is not saved with the file.
Stop is next quickest, but is unconditional. It is saved with the file.
Debug.Assert is slower to implement because the expression has to be written to return a True or False.
Debug.Print takes time to implement, but is most informative because any information may be included in the output. The Debug.Print output in the Immediate Window survives after the UserForm closes, but not the closing of Excel. The Immediate Window buffer is of limited size, and earlier Debug.Print outputs are deleted as buffer memory is needed.
Log files survive after the UserForm closes, and also the closing of Excel. Log files are needed if the Immediate Window buffer is not large enough to hold all the Debug.Print output..
|
OzGrid.com accepts no responsibility for any adverse affect that may result from undertaking our training.