Introduction
This article will take you through the fundamentals, techniques, functions and utilities for performing error debugging and macros in Excel VBA.
Place a 'command button' in the worksheet then add the following lines below. To execute the lines, click the command button on the sheet.
Debugging
The various strategies for debugging programs and identifying errors can be as follows:
- Single Step - Using the F8 key, one can single-step through their program/snippet in VBA. This will execute each line independently as one goes through the effects of each line as they go through the program.
To start debugging the program, place a new command button in your sheet and try debugging the following program.
Code
Dim i As Integer, j As Integer
For i = 1 To 2
For j = 1 To 5
Cells(i, j).Value = WorksheetFunction.RandBetween(20, 100)
Next j
Next i
Result

This would be the result of running the above program.
Now to see the output and working of each line of the above program, one can
- Empty the range of cells from A1 to E2 and resize the VB editor side by side with the sheet to see the debugging output of each statement in the program.
- In the VB editor, place a cursor at the start of the function and press F8. This should change the colour of the first line to yellow.

3. Press the F8 key four times. For i = 1 and j = 1, in excel VBA enters a new random number between 20 and 100 in the cell at the intersection of row one and column one. By holding the cursor steady on a variable, one should be able to see the value of the variable.

4. Now Press F8 two more times. For i = 1 and j = 2, in excel VBA should enter another random number between 20 and 100 in the cell at the intersection of row 1 and column 2.

5. Now single-step through the rest of the program to see how Excel VBA enters the other numbers. This is an excellent way if one wants to learn how a loop works. If you wish to stop the program, click the Reset (Stop) button.
2. Breakpoint - A breakpoint can be used and set to halt/pause the execution of the running program at a specific line.
- For the same program used above, first, empty the range of cells from A1 to E2.
- Set a breakpoint by clicking on the left margin (in grey) where you might want to place your breakpoint. You should be able to see a red dot appearing there.

3. Keep clicking on the green arrow to execute the macro until the breakpoint is reached.
