Home > Macros > Basics > Troubleshoot

Excel Macro Troubleshooting Tips

Troubleshoot a simple Microsoft Excel macro, if an error message appears. Use the Debug button to find and fix a small problem in the code. How to fix the problem in the F8 key stops working while stepping through a macro. Solution for Excel run-time error - Invalid Forward Reference. What to do if the Project Explorer list is missing.

error message Invalid forward reference

Error Message Debug Button

Occasionally, when you try to run a macro, an error message will appear, such as the "Run-Time Error '9': Subscript Out of Range" error shown below.

  • Title Bar: Microsoft Visual Basic
  • Error Description: Run-time error '9': Subscript out of range

microsoft Visual Basic run-time error 9 subscript out of range

Error Message Buttons

On this error message, there are 3 buttons available -- End, Debug and Help.

  • End: To stop the macro, click the End button.
    • Message closes, and the macro stops running.
  • Help: To get information about the error, click Help.
    • Error message stays open
    • An information on the Microsoft website opens, in your default web browser
    • For the error message shown above, the help page is: Subscript Out of Range (Error 9)
      • Like most of the Microsoft help pages, this one shows a list of possible causes for the erroe, and suggests solutions for fixing the error
  • Debug: To go to the VBE and try to solve the problem, click the Debug button.
    • This is the button that I usually click, because it takes me to the macro's VBA code, at the line were the macro stopped running. See the next section for Debug button details.

Debug an Excel Macro

In this example, the Excel macro was recorded, while I did the following steps:

  1. Go to the worksheet named "Main"
  2. On that sheet, select cell C3

Here is the VBA code, for the GoToMainSheet macro:

Sub GoToMainSheet()
' GoToMainSheet Macro
    Sheets("Main").Select
    Range("C3").Select
End Sub

Debug Macro Step by Step

Later, when I ran that recorded macro, using the above code, Excel showed me an error message, with this text:

  • Run-time error '9': Subscript out of range

In that message, I followed these steps, to troubleshoot the problem:

  • In the error message, click the Debug button
  • The Visual Basic Editor (VBE) opens, showing the GoToMainSheet macro
  • In the macro, a line of code is highlighted in yellow
  • There is a yellow arrow in the margin at the left of the highlighted line of code.

The highlighted line, in the below code, is where the macro stopped running.

  • When it tried to run the highlighted line, there was a problem.
  • It could not complete the step in that line of code.

line of code highlighted in yellow

Check the Help Page

Now that you know which line of code caused the problem, you can check the Help page, to see if it gives you any clues about the cause of the error, and fixing the problem.

To go to the Help page, follow these steps:

  • To continue running the macro again, press the F5 key on your keyboard
    • Or, at the top of the VBE window, click the Run menu, then click the Continue command
    • click Continue command on VBE Run menu
  • When the error message appears, click the Help button, to go to the Microsoft help page
  • In this example, the help page has a list of 4 possible causes and fixes for the error.
    • Note: If you are new to Excel macro programming, that list might not be too helpful to you!
  • Two of the causes mention "nonexistent" items, so perhaps Excel can't find the sheet named "Main".
  • Next, I'll check the Excel workbook, to see if there is a problem with that sheet name

Get Ready to Fix Code

After you check the Help page, follow these steps, so you can start fixing the problem:

  • Go back to Excel, in the VBE
  • Click the End button in the error message, so you can check on the sheet name.

OR, if there is still a line of code with yellow highlighting, follow this step:

  • At the top of the VBE window, in the toolbar, click the Reset button (a blue square)
  • The code stops running, and the yellow highlighting disappears

reset button on toolbar

Check Sheet Names

Next, to check the workbook's sheet names, you can use the Project Explorer, at the left side of the VBE window.

  • First, look in the Project Explorer window, and find the workbook with the GoToMainSheet macro
  • Click the plus sign beside the Microsoft Excel Object folder, to see its contents
  • In the list of objects, check the sheet names for the workbook.
    • In the screen shot below, there are 3 sheets: "Intro", "Sheet2" and "Sheet3"
    • There is NO sheet named "Main"
    • After the macro was recorded, the "Main" sheet was renamed, as "Intro"
    • That made the "Main" sheet "nonexistent", so the macro can't run the line that refers to the "Main" sheet.
  • check list of sheets

Fix the Code

Next, you'll edit the VBA macro code, to change the sheet name.

Solution 1: Not Recommended

  • In the line of code, you could replace "Main" with "Intro", which is the current name of the first sheet in the workbook.
  • However, with that solution, the code could show an error again, if anyone changes the name of the worksheet in the future.
  • This is not the best solution
Sheets("Intro").Select

Solution 2: Recommended

Instead of using the sheet name that you can see on the Sheet tab, we'll use the sheet's Code Name. The sheet's Code name is much less likely to be changed, by anyone other than the programmer.

In the Project Explorer, you can see each sheet's Code Name, followed by the name that appears on its sheet tab.

  • For example, in the screen shot below, the first sheet shows: Sheet1 (Intro)
    • Code name is Sheet1 - can be changed in VBE window
    • Sheet name is Intro - shown on sheet tab in Excel, easy to change in Excel

check list of sheets

Change Macro to Use Code Name

The first sheet's Code Name is "Sheet1", so change the macro's code to use that name, as shown below.

This solution is better, because the code will continue to run, even if someone changes the name on the worksheet's tab in Excel.

Here is the revised code, for the GoToMainSheet macro:

Sub GoToMainSheet()
' GoToMainSheet Macro
    Sheet1.Select
    Range("C3").Select
End Sub

Save Changes and Test Macro

After you finish the macro changes, follow these steps to make sure the revised code is working correctly now:

  • On the VBE Toolbar, click the Save button
  • Next, close the VBE window, and return to Excel
  • Run the macro again, and it should work correctly, without showing a runtime error.

Fix Missing References in VBE

Some Excel errors, such as "Run-time error 1004: Application-defined or object defined error", can be caused by missing VBA References on your computer.

To find and fix that problem, follow these steps:

  • In the Visual Basic Editor, on the menu bar, click the Tools menu
  • Next, in the drop-down list of commands, click References
  • If one of the references is marked as MISSING, remove its check mark
  • Then, look at that reference name, and scroll down the list, to see if there is a different version of that reference.
  • If you find one, add a check mark to it
  • Then click OK, to close the dialog box, and see if that fixes the problem.

F8 Key Stops Working

When debugging Excel VBA code, you can use the F8 key, or the Step Into command, to step through the code, one line at a time.

regist add dword

However, in Excel 2010 or later versions, you might encounter the problem where the F8 key (or Step Into) stops working, part way through a procedure.

Sometimes, this occurs after the code opens another file. Instead of stopping at the next line of code, it runs to the end of the procedure, or to the next breakpoint.

Change the Registry

To fix this problem, you can make the following change in the Registry. As always, make a backup of the Registry, before making any changes, and try this at your own risk!

Thanks to Ross Connell, for sharing this solution, that he learned from Microsoft Support.

The registry change affects RPC Debugging, and you can read more about it on the Microsoft website:

1) Make a Backup

Before you make any changes to the registry, be sure to do a backup first!

2) Find Registry Key

Next, open the Registry, and go to the applicable VBA key, based on your version of Office and Window

  • For 32-bit Office on 64-bit Window go to registry key:
    • HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VBA
  • For 32-bit Office on 32-bit Window go to registry key:
    • HKEY_LOCAL_MACHINE\SOFTWARE\ Microsoft\VBA
  • For 64-bit Office on 64-bit Windows go to registry key:
    • HKEY_LOCAL_MACHINE\SOFTWARE\ Microsoft\VBA

What To Do Next

  • If you found the applicable VBA key, go to step 3, where you will create a DWORD.
  • If you did NOT find the key, try the solutions in the next section, to search the registry or create the key manually.

No VBA Key in Registry

If you can't find a VBA key in the expected location, try one of the following solutions

1. Search the Registry

Try this solution first - it's easier! (Thanks to Peter Tanner for this tip)

  • In the Registry, right-click on the HKEY_LOCAL_MACHINE folder
  • In the pop-up menu, click Find
  • In the Find box, enter: Microsoft\VBA
  • Click the Find Next button

If that didn't work, try the next solution, below.

search for microsoft vba folder in registry

2. Create the VBA Key Manually

If you can't find the Microsoft\VBA key anywhere in the Registry, follow the steps below, to create the key manually. (Thanks to Ross Connell, for sharing this additional tip)

  • First, find the folder where the VBA key should be located, based on your version of Office and Windows.
  • In the list at the left, select that folder, to see its contents
  • Next, right-click in the right pane of the window, where the contents are listed.
  • Then, in the pop-up menu, click New, then click Key
  • Next, rename the new key as VBA
  • Then, in the list at the left, select the new VBA key, to see its contents

right-click to create a new key

Note: Other Locations

Thanks to Lutz Fricke, who let me know that other locations in the registry might work too. Lutz couldn't find a Microsoft\VBA key, and created one in this location, which solved the F8 key problem on that computer:

  • Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ Office\ClickToRun\REGISTRY\MACHINE\ Software\Wow6432Node\Microsoft\VBA

3) Create DWORD in VBA Key

Finally, follow these steps, to create the DWORD (or QWORD) in the VBA key.

  • First, right-click in the right pane of the window, where the contents are listed.
  • Then, in the pop-up menu, click New
  • In the pop-up menu, click DWORD or QWORD, depending on your version of Office

right-click to create a new key

  • Next, name the DWORD (or QWORD) as DisableOrpcDebugging7
  • Right-click the DWORD (or QWORD), and click Modify
  • Change Value Data to 1, and leave the other settings as they are
  • Then, click the OK button, to complete the changes

edit the DWORD

View New Item in Registry

The completed DWORD or QWORD will appear in the Registry.

For example, this screen shot shows a DWORD

completed DWORD

This screen shot shows a QWORD, in a manually created VBA folder

completed QWORD

4) Close Registry

After you finish the steps to create the new DWORD or QWORD in the Microsoft\VBA key, follow these steps:

  • Close the Registry
  • Re-open Excel
  • Test the F8 key, to see if it now works correctly, stepping through the code, and stopping at each line.

Project Explorer Folders Missing

When you work in the Visual Basic Editor, the Project Explorer is usually visible, showing a list of all the open VBA projects.

Click the + or - button at the left of a project name, to open and close the list of objects for that project.

regist add dword

NOTE: If a project is protected, and hasn't been unlocked during the current session, you will be prompted for a password when you click the + button.

regist add dword

Show Folder Contents

Then, click the + or - button at the left of a folder name, to open and close the list of objects for that folder.

regist add dword

Folders Are Missing

Occasionally, the project explorer folders might disappear, and an alphabetical list of the workbook's objects appears below the project name.

regist add dword

To show the objects grouped into folders again, follow these steps:

  1. Click on the project name, for an unlocked project
  2. In the toolbar at the top of the Project Explorer window, click the Toggle Folders button.

The folders will reappear for all projects, with objects groups into folders.

regist add dword

Error: Invalid Forward Reference

Sometimes Excel files work perfectly for years, and suddenly have macro problems. In the sections below, I've put details on an "Invalid Forward Reference" Automation error that suddenly appeared, and how I fixed it, with help from a Stack Overflow thread.

  • a) Excel Workbook Used Daily
  • b) Run-Time Error Message
  • c) Invalid Forward Reference
  • d) Solution: Invalid Forward Reference Error
  • e) Possible Causes for Error

a) Excel Workbook Used Daily

I have a "Daily Totals" workbook, that I use every morning, to keep a record of key numbers.

The file has two worksheet buttons that run simple macros, and an Event Procedure that runs before the workbook closes.

worksheet buttons that run simple macros

b) Run-Time Error Message

Recently, an Excel error message appeared, when I tried to close the workbook:

  • Run-time error '-2147319767 (80028029)': Automation error Invalid forward reference, or reference to uncompiled type

I clicked the OK button, and went to check the workbook VBA code.

error message Invalid forward reference

c) Invalid Forward Reference

In the macros, there were worksheet references, and named range references, and all of those were valid.

The same "Invalid Forward Reference" error message appeared with a simple in the immediate window for this code:

  • Debug.Print activesheet.name

I closed the workbook without saving the changes, and searched Stack Overflow for a solution.

d) Solution: Invalid Forward Reference Error

Fortunately, lots of other people have encountered this problem, and I'm grateful to Notus_Panda, who posted a simple solution:

  • In the Visual Basic Editor, go to the Tools menu, and click References
  • In the list of references, enable any unchecked ref, e.g. AccessibilityCplAdmin 1.0 Type Library
  • Note: You can uncheck that reference later, if you want to
  • Next, compile the code, and save the file.
  • Finally, close the workbook, and then re-open it.

I tried that solution, and it worked!

NOTE: There are other solutions on the Stack Overflow thread, so take a look at those too, if this solutions doesn't solve the problem in your Excel file.

e) Possible Causes for Run-Time Error

This is a mysterious VBA error, and in the same Stack Overflow thread, pgSystemTester posted an explanation, with a few possible causes.

  • Debugging - I don't remember debugging this file prevously
  • Excessive event macros - the workbook only has one event procedure
  • Excel/VBA add ons - I don't use MZ-Tools, but have a couple of my own add-in enabled
  • Other VBA libraries - None used

So, I'm not clear on what caused this error in my workbook, but I'm glad it was easy to fix!

Get Sample File

Debug Button: To test the VBA debugging steps, get the Error Message Debug Button sample file. The zipped Excel file is in xlsm format, and contains the macro named GoToMainSheet. To test the macro, be sure to enable macros when you open the workbook

More Tutorials

Getting Started with Excel Macros

FAQs, Excel VBA, Excel Macros  

Adding Code to an Excel Workbook

Worksheet Macro Buttons

Create a Worksheet List Box

Excel Macro Humour

I can relate to this problem. And sometimes it's a macro that I wrote a day earlier! How about you?

Reading an Excel macro you wrote 5 years ago

Last updated: March 4, 2024 9:16 AM