Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Debugging a Macro.

Debugging a Macro

Written by Allen Wyatt (last updated February 11, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


6

In Excel, macros are written in a language called Visual Basic for Applications, or VBA. When you write a macro, you need to test it and correct any errors in the macro. This process is called debugging. The process of debugging a macro in VBA is the same as debugging in any other programming language. All you need to do is step through the macro, one command at a time, and make sure it works as you think it should. You do this by viewing both the windows for your macro and a test worksheet. As you step through the macro (using the commands available in the Debug menu of the VBA Editor), you can correct any errors you locate. (I particularly like to use the F8 key to step through the macro one line at a time.)

As you are debugging macros, you need to make sure you think through every possible way the macro could be used and all the possible conditions that could exist at the time the macro is invoked. Try the macro out in all these ways and under all these conditions. In this way, you will make your macro much more useful.

Don't be surprised, however, if you give your workbook to some friends and they discover bugs you never thought of. In those cases, the debugging process is the exact same as mentioned above—except you use their data as your test worksheet. Try to go through the macro using their data, one line at a time, until you discover where your code went wrong and then fix it.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7732) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Debugging a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Extracting Foreign-Language Characters

If you need to analyze some text to determine if it contains accented characters, there are a couple of ways you can ...

Discover More

Checking for Matching Parentheses

There are lots of little "gotchas" that can make the difference between a finished document and a polished document. One ...

Discover More

Sorting ZIP Codes

Sorting ZIP Codes can be painless, provided all the codes are formatted the same. Here's how to do the sorting if you ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Highlighting Pattern Violations

A common part of working with text strings in a worksheet is normalizing those strings so that they follow whatever rules ...

Discover More

Using the Status Bar

When developing a macro, you may want to display on the status bar what the macro is doing. Here's how to use this ...

Discover More

Sharing Macros with Others

If you develop some handy macros that you use on your system, you may want to share those macros with others. This tip ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is three minus 2?

2023-02-12 11:59:41

Tomek

@Philip:
Thank you for your reply.
I don't think infinite loop is a cause, because other than non-responsive shortcut keys everything else seems to work. I can still step through the code or run the rest of the code by using the buttons on the debug toolbar. I can stop the code and edit it, except that Ctrl+C and Ctrl+V do not work, I have to use shortcut menu.

Thank you for you offer of help. I may contact you when I encounter this behaviour next time.


2023-02-12 02:35:13

Philip

@Tomek, in my experience this probably means that the step right before everything freezes is a step that launches you into an infinite loop.

What I try to do in those cases, is (after a reboot of Excel) step through the code again and note exactly which was the last "successful" step. Not sure if this is helpful ... if you could let me have an example of scenario where you encounter the issue, I could try looking for a cause here on my system? (no strings attached ... just one VBA developer helping out another one)

Let me know if I can be of help (if you want to contact me, please use the contact form on my web site pi-productivity.be ...)


2023-02-11 23:50:58

Tomek

@John Louws,
Not that you spoiled it for me, as I have already solved Wordle 602 in 5 moves, but people on the other coast of USA or Canada still have over 3 hours , and those in Hawaii more than 6 to solve it.


2023-02-11 23:22:48

John Louws (Australia)

Hi Allen,

Thank you for the years of Excel tips; but this weeks debugging tip was also on the mind of the Wordle people as game 602 on 11 Feb 2023 the word was 'debug'. Although sometimes applying to a Macro it is easier than to Wordle ... no F8 key in Wordle for resolution... ha ha.

Regards,

John


2023-02-11 18:26:10

Allen

I haven't had that come up, Tomek.

-Allen


2023-02-11 13:45:05

Tomek

I would like to ask Allen, as well as the readers of this tip if anyone encountered a problem that occasionally pops up on my system:
While debugging a macro and using F8 to step through it, sometime in the middle of the macro this key will stop working. Usually at the same time my F5, Ctrl+C, Ctrl+V and some other Windows shortcuts will also stop working. I can still use the buttons on the Debug Toolbar to step through the macro, and Copy/Paste from the right-click context menu. The problem disappears when I close then reopen Excel.

Anyone knows how to fix this?


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.