Microsoft Office Tutorials and References
In Depth Information
Testing and Debugging Your Functions
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:
Function VOWELCOUNT(r)
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
End If
Next i
End Function
In this case, the value of two variables ( Ch and i ) print to the Immediate window whenever the
Debug.Print statement is encountered. Figure 23-7 shows the result when the function has an
argument of North Carolina .
When your function is debugged, make sure that you remove the Debug.Print statements.
Search JabSto ::

Custom Search