Debugging in Excel VBA - Step by Step Tutorial
Debugging in Excel VBA - Step by Step Tutorial
For i = 1 To 2
For j = 1 To 5
Cells(i, j).Value = WorksheetFunction.RandBetween(20, 100)
Next j
Next i
see the Visual Basic Editor and worksheet at the same time.
3. In the Visual Basic Editor, place your cursor before Private and press F8.
The first line turns yellow.
4. Press F8 four times. For i = 1 and j = 1, Excel VBA enters a random number
between 20 and 100 into the cell at the intersection of row 1 and column 1. By
holding the cursor steady on a variable, you can see the value of the variable.
5. Press F8 two more times. For i = 1 and j = 2, Excel VBA enters a random number
between 20 and 100 into the cell at the intersection of row 1 and column 2.
6. Single step through the rest of the code to see how Excel VBA enters the other
numbers. This is an excellent way to learn how a loop works. If you wish to stop
the program, click the Reset (Stop) button.
✦ Breakpoint
You set a breakpoint to halt execution at a specific code line.
1. Empty the range A1:E2.
2. To set a breakpoint, click on the left margin (in grey) where you want to place
the breakpoint. A red dot appears.
3. Click on the green arrow to execute the macro until the breakpoint.
Result:
Chapter
Macro Errors
Learn more, it's easy
Debugging
Error Handling
Err Object
Interrupt a Macro
Subscript Out of Range
Macro Comments
Download Excel File
debugging.xlsm
Next Chapter
String Manipulation
Follow Excel Easy
Become an Excel Pro
1. Introduction
2. Basics
3. Functions
4. Data Analysis
5. VBA
Debugging • © 2010-2025
March 2025 Suggestions: FILTER function • Quick Analysis • Wildcards • Loop (VBA) • Split
Cells