Microsoft Office Tutorials and References
In Depth Information
If you get tired of seeing the message boxes, you can halt the code by pressing
Ctrl+Break. Then, respond to the dialog box that's presented. Clicking the End button
stops the code. Clicking the Debug button enters Debug mode, in which you can step
through the code line by line.
To display more than one variable in a message box, you need to concatenate the variables and insert a space
character between each variable. The following statement, for example, displays the value of three variables (x,
y, and z) in a message box:
MsgBox x & “ “ & y & “ “ & z
If you omit the blank space, you can't distinguish the separate values.
Alternatively, you can separate the variable with vbNewLine, which is a constant that inserts a line break. When
you execute the following statement, x, y, and z each appear on a separate line in the message box.
MsgBox x & vbNewLine & y & vbNewLine & z
Using Debug.Print statements in your code
If you find that using MsgBox statements is too intrusive, another option is to insert some temporary code that
writes values directly to the VB Editor Immediate window. (See the sidebar, “Using the Immediate window.”)
You use the Debug.Print statement to write the values of selected variables.
For example, if you want to monitor a value inside a loop, use a routine like the following:
Dim Count As Long, Ch As String
Dim i As Long
Count = 0
For i = 1 To Len(r)
Ch = UCase(Mid(r, i, 1))
If Ch Like “[AEIOU]” Then
Count = Count + 1
Debug.Print Ch, i
VOWELCOUNT = Count
In this case, the value of two variables (Ch and i) print to the Immediate window whenever the Debug.Print
statement is encountered. Figure 24-7 shows the result when the function has an argument of North Carolina.