More Tips for Debugging VBA

Philip Treacy

May 12, 2017

After my last post on debugging VBA I received a few comments with other tips or commands people use for debugging. I'm going to have a look at these here.

Option Explicit

The first thing is using the Option Explicit statement. There are a number of Options you can use in VBA, and Option Explicit tells VBA that every variable needs to be declared before it is used.

I'm not sure why this isn't a default really as using variables that aren't declared can only lead to trouble. Let's look at an example.

Let's say we have a variable called val and we to use it calculate a result.

Sub MoreDebugging()

    Dim result as long

    result = val * 2

End Sub

If you run this without the Option Explicit statement, the code will run, but result will contain 0 as val has not been declared or assigned a value.

This seems obvious in a simple example like this, but if you have dozens, or hundreds of lines of code, and you use variables without declaring them, it can lead to all sorts of problems.

Adding Option Explicit to the top of the code module tells VBA to check that all variables are declared, and if they are not, you'll get a 'Variable not defined' error.

Option Explicit

Sub MoreDebugging()

    Dim result as long

    result = val * 2

End Sub

Stop

In my last post I talked about breakpoints. Using the Stop statement is like using a breakpoint, except it's part of the code and will remain there until you remove it.

Option Explicit

Sub UsingStop()

    Debug.Print "Hello"

    Stop

    Debug.Print "Goodbye"

End Sub

When you execute the code above, the code pauses at the line where Stop is. You can then step through the code (F8) or continue execution (F5).

Make sure you remove any Stop statements from your code after you are finished writing or debugging it.

Debug.Assert

Using this is like using a conditional breakpoint. If a certain condition is met, then break (pause code execution).

Option Explicit

Sub Assert()

    Dim num as integer

    num = 1

    Debug.Assert num > 0

End Sub

In the above code num is given the value 1. Debug.Assert will pause execution if the test num > 0 is false.

This is a funny one because it is halting code execution if the test condition evaluates to False, rather than True as you might expect.

? instead of Debug.Print

This one only works in the Immediate Window, not in your code.

To check the value of something you can type debug.print SomeVar and the value is shown in the Immediate Window.

Rather than type debug.print all the time, just use ? e.g. ? SomeVar will print the value of SomeVar

MsgBox

Personally I don't use this much as I don't like my code being interrupted and then having to click a button on a message dialog before execution can continue. I just prefer using debug.print statements. But it is one option for you to see the value of variables.

Rather than using debug.print to show things in the Immediate window, use MsgBox to display things in the Excel application.

Option Explicit

Sub MyMsgBox()

    Dim num as integer

    num = 1

    MsgBox "The value of num is " & num

End Sub

AUTHOR Philip Treacy Co-Founder / Owner at My Online Training Hub

Systems Engineer with 30+ years working for companies like Credit Suisse and E.D.S. in roles as varied as Network & Server Support, Team Leader and Consultant Project Manager.

These days Philip does a lot of programming in VBA for Excel, as well as PHP, JavaScript and HTML/CSS for web development.

He's particularly keen on Power Query where he writes a lot of M code.

When not writing blog posts or programming for My Online Training Hub, Philip can be found answering questions on the Microsoft Power BI Community forums where he is a Super User.

3 thoughts on “More Tips for Debugging VBA”

    • Well, people warn against Stop for good reason, as it could cause your code to bomb out later (e.g., when someone else is using your workbook), if you ignore Philip’s injunction: “Make sure you remove any Stop statements from your code after you are finished writing or debugging it.”
      And let’s face it, it’s easy to forget to go back and remove all the stops.
      But there is a middle road, because there are some contexts where Stop is just plain useful.

      Reply

Leave a Comment

Current ye@r *