Yes, Excel VBA can run line by line. This is most commonly achieved by using the Immediate Window debugging tools or the Breakpoints feature within the VBA Editor. You do not need any special add-ins; the functionality is native to the software. To begin, you must have the VBA Editor open and your specific procedure loaded into memory, as the debugger cannot stop code that is not currently in the active project.

What You'll Need

  • Microsoft Excel installed on a Windows PC (VBA is not available on macOS by default).
  • A spreadsheet with at least one existing macro or a new module where you will write code.
  • The ability to access the Developer tab in the ribbon, which contains the VBA Editor shortcut.

Step-by-Step

  1. Open the VBA Editor. In Excel, go to the Developer tab on the ribbon. If this tab is not visible, go to File > Options > Customize Ribbon and check the box for Developer. Once visible, click the Visual Basic button to launch the editor.
  2. Locate Your Code. In the left-hand pane, navigate to the project explorer (usually on the left side). Expand the folders until you find the specific module or worksheet object that contains the code you wish to debug. Click on it to ensure it is active.
  3. Insert a Breakpoint. The most common method to run code line by line is to set a breakpoint. Look at the margin on the far left of the code window, next to the line numbers. Click the left mouse button on the gray area immediately to the left of the specific line of code where you want execution to stop and begin stepping through. A dark dot will appear in that margin, indicating the breakpoint.
  4. Run the Procedure. Return to the Excel window. Press Alt + F8 to open the Macro dialog box. Select the macro you just set the breakpoint on and click Run. Alternatively, you can press F5 directly from the VBA Editor while the cursor is inside the procedure.
  5. Enter Break Mode. When the breakpoint is reached, the code will stop executing, and the VBA Editor will enter Break Mode. The cursor will appear on the breakpoint line. The background of the code window will typically turn gray to indicate this state.
  6. Use the Immediate Window. With the code stopped, look for the Immediate Window. This is usually located below the code window or to the side of it. If it is not visible, go to the menu bar and click View > Immediate Window. You can now type VBA commands directly into this window.
  7. Step Through Code. To move one line at a time, press the F8 key repeatedly. Each press of F8 will execute the current highlighted line and move the cursor to the next line. You will see variables change values and logic flow move down the script in real-time.
  8. Inspect Variables. While in break mode, you can hover your mouse over any variable name in the code window to see its current value. In the Immediate Window, you can also type the variable name (e.g., rng) and press Enter to print its value directly to the window.
  9. Resume Execution. Once you have debugged the issue and are satisfied with the line-by-line execution, you can press F5 again to resume normal execution of the macro from that point forward.

Common Mistakes

A frequent error is attempting to set breakpoints on lines that contain no executable code, such as comments (lines starting with an apostrophe) or variable declarations. The debugger cannot stop on a comment, so you must place the breakpoint on an actual line of code that performs an action, like an assignment or a function call.

Another common issue arises when the macro runs too fast for the human eye to follow. Even with breakpoints, if the code loops through thousands of rows, you might still miss the exact moment a value changes. In these cases, you must insert temporary Stop statements within the code itself, which act like a breakpoint that you set manually.

Tips

Utilize the Locals Window (View > Locals Window) alongside the Immediate Window. The Locals Window automatically populates with all variables defined in the current scope, providing a snapshot of your data structure at the exact moment the code pauses.

For longer procedures, use the Step Into (F8) command to move down one line, but be aware that if your code calls a custom function or subroutine, F8 will drill down into that function. To skip over called functions and continue with the next line in the main procedure, use the Step Over command, usually found on the Debug menu or via a shortcut key.

Troubleshooting

If pressing F5 or F8 does not start the debugger, ensure that the Break on Unhandled Errors option is enabled. Go to Tools > Options > General and check the box for Break on Unhandled Errors. Without this enabled, the debugger will often skip over errors to the end of the procedure, making it impossible to step through the code line by line.

If the Immediate Window is empty or unresponsive, check that the code is actually in Break Mode. If the macro finished successfully, the code will have exited the procedure and returned to normal Excel operation. You must press F5 again to re-enter the procedure and start the stepping process.

Frequently Asked Questions

Why does the cursor not move when I press F8?

This usually happens because the code you are trying to debug is not currently running. Ensure that you have pressed F5 to run the procedure and that you have actually reached a breakpoint. If the macro is currently executing a slow operation, you may need to wait for it to complete or use the Stop statement to force it to pause.

Can I run line by line on a Mac?

No. Visual Basic for Applications (VBA) is not natively supported on macOS. While there are limited third-party emulators or workarounds, the standard Excel VBA Editor and its debugging tools (F8, breakpoints) are exclusive to Windows versions of Microsoft Office.

What is the difference between Step Into and Step Over?

Step Into (F8) moves to the next line of code, including any functions or subroutines called by the current line. Step Over moves to the next line in the current procedure but executes the called function entirely in the background without pausing inside it.

How do I remove a breakpoint?

Simply click on the gray dot (the breakpoint marker) in the margin again. The dot will disappear, and the code will run normally without pausing at that line. Alternatively, you can press Ctrl + Shift + F9 to clear all breakpoints in the current module.

Can I add a Stop command to my code permanently?

Yes. You can type the word Stop on its own line in the VBA Editor. When the macro runs, it will pause at that line just like a breakpoint. Remember to delete or comment out these lines before you distribute the file to others, as they will cause the macro to halt unexpectedly.

Why does the code stop on a line that says Debug.Print?

This is not a bug. The Debug.Print statement is a valid executable line in VBA. It prints data to the Immediate Window but does not perform a visible action in Excel. The debugger will pause on this line so you can see the output of the print command in the Immediate Window.