| | VBA Tutorial
In this VBA Tutorial we'll explain
how debug your code. Debugging is running your code step by step from
where you decide.
- Open an new
Excel WorkSheet (or Word document) and press F11 to open the VBA Editor.
- Right
click on any blank space among the toolbars.
- Click
on Debug. The Debug menu bar will display. If it displays as a floating
bar and not as a fixed toolbar, drag it into the toolbar area.
- Do
the same for the Edit menu.
- Right click on any
branch in the VBAProject Explorer tree and click Insert
> Module.
- Copy the
following code and Paste it in the Module.
Sub
DebugExample() num =
InputBox("Enter an integer with 1 to 6 digits")
num = 1579 If IsNumeric(num)
Then
If InStr(num, ".") Then
MsgBox "Please enter an integer with 1 to 6 digits."
Exit Sub
Else
ilen = Len(num)
If ilen > 6 Or ilen < 1 Then
MsgBox "Please enter an integer with 1 to 6
digits."
Exit Sub
End If
End If Else
MsgBox "Please enter an integer with 1 to 6 digits."
Exit Sub End If
If ilen > 3 Then
iRight3Digits = Right(num, 3)
iLeftDigits = Left(num, ilen - 3)
End If num = iLeftDigits
& "," & iRight3Digits End Sub
note:
there is a much easier way to put a comma in a number using the Format
command - this VBA tutorial is about how to debug your code.
- Place
the cursor (click your mouse) anywhere in the 2nd line...
num = InputBox("Enter an
integer with 1 to 6 digits") and click
the Comment
Block Icon that is the Edit menu we added in step 4.
This
will place an apostrophe at the beginning of the line. A line that
starts with an apostrophe as the first character in the line is
considered a Comment by VBA. Since we are going to debug this code in
this VBA tutorial, we will assign numbers to a variable instead of
popping up an Inputbox. This will save time. After we are sure our code
works, we can uncomment the line and Comment or delete the line that
assigns a value to the variable. Commented lines appear in green for
easy identifiability. - At the end of the code also
comment out the line MsgBox
num.
- Place the cursor anywhere within
the code from the line...
Sub
DebugExample() to the line... End Sub Inclusively. - Press
F8 or click the Step
Into button in the Debug menu we added in Step 3.
The
first line of the code will now be highlighted in yellow... - As
you click Step into or press F8 the code will advance line by line
according to the logic of your code. In our example we are setting num
to 1579 which is an integer with less than 7 digits so we will bypass
all the Message Boxes that inform the user to enter an appropriate
value.
- Keep stepping through the code until the
last line End Sub
is highlighted in yellow.
- Hover your mouse over the
variable iRight3Digits
(near the end) and you will see the value it holds. Do this also for
the variables iLeftDigits
and num.
In
this VBA tutorial and its example, calculating the length of the sides
to the left and right of the comma was easy. If you were trying to, for
example, get the first and last name from a string like "John, Doe" you
might not calculate this correctly
Sub names()
a = "John,Doe" firstname =
Left(a, InStr(a, ",") - 1)
lastname = Right(a, Len(a) - InStr(a, ",")) End Sub
Say
you forgot to put the -1
at the end of the firstname line. When you hover the mouse over the
variable firstname
you would see "John,"
While you are in Debug mode
you can change you code to correct this.
Then Right
click on the line you corrected and click Set Next Statemant.
One more tip to end this VBA
Tutorial... If the procedure that you want to debug
is called from another procedure, you can enter Debug mode by typing Stop on an empty
line or by setting a breakpoint by either... - Clicking
on a line and and then pressing F9. Or...
- Click
on the grey margin to the left of a line.
This ends this VBA
Tutorial.
Go
to the next tutorial:
How to use the VBA Timer
Return
to our Homepage
|