Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. 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, 2021, and Excel in Microsoft 365


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, 2021, and Excel in Microsoft 365. 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

Working with Form Fields

You know you want to use form fields in your document (they are essential in creating forms, after all) but you need to ...

Discover More

Quickly Changing Font Sizes

A quick little shortcut can help you easily step through different font sizes for whatever text you've selected. Word ...

Discover More

Appending to a Non-Document Text File

Your macros can easily add information to the end of an existing text file. This is done by opening the target file in ...

Discover More

Program Successfully in Excel! This guide will provide you with all the information you need to automate any task in Excel and save time and effort. Learn how to extend Excel's functionality with VBA to create solutions not possible with the standard features. Includes latest information for Excel 2024 and Microsoft 365. Check out Mastering Excel VBA Programming today!

More ExcelTips (ribbon)

Calculating the Distance between the Top of the Window and Row 1

Normally Excel positions a UserForm in the center of your screen. You may want to position the form elsewhere, more ...

Discover More

Displaying the Print Dialog Box in a Macro

Want to print a document by using a macro? One way is to display the Print dialog box and allow the user to interact with ...

Discover More

Updating Automatically when Opening Under Macro Control

If your workbook contains links, you are normally given the opportunity to update those links when you open the workbook. ...

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 two more than 6?

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.