VBA Error 9 Subscript Out of Range: Debug Guide

VBA editor showing a highlighted subscript out of range error

If VBA stops with “Run-time error 9: Subscript out of range,” the macro requested an item that does not exist in an array, workbook, worksheet, or collection. The most common cause is a worksheet or workbook name in the code that does not exactly match the name available at runtime; vba error 9 subscript out of range is a reference mismatch, not usually an Excel installation problem.

Why this happens

VBA resolves an index or key against the items that actually exist at that moment. If the requested position or name is missing, it cannot return an object or value and raises error 9.

  • An array index is outside its declared bounds, or the array has not been initialized.
  • A workbook, worksheet, or other collection item is requested by a name or index that is not present.
  • The code refers to the wrong collection or a different active document than intended.

Use the table to narrow down the first check:

Likely causeTypical symptomQuick test
Worksheet name mismatchError on a Sheets or Worksheets lineCompare the quoted name with the visible sheet tab
Wrong workbook referenceCode works only when a particular file is openInspect Workbooks in the Immediate window
Array bounds mismatchError on an array element such as values(i)Check LBound and UBound before indexing
Collection index does not existError on an indexed item in a loopCheck the collection's Count and loop limits

1. Read the highlighted line

When the error dialog appears, choose Debug. The VBA editor highlights the statement that failed. Identify the expression inside brackets or after a collection name, such as Worksheets("Data"), Workbooks("Report.xlsx"), or items(i). If the dialog is gone, run the macro again from the editor with F5 and choose Debug when prompted. The highlighted statement tells you which reference to inspect first.

2. Match worksheet names exactly

In Excel, compare the sheet tab text with every name used in the failing line. Spaces, punctuation, and renamed tabs matter. For example, Worksheets("Sales Data") will fail if the actual tab is named Sales. Check that the macro is using the intended workbook too: qualify the reference, such as ThisWorkbook.Worksheets("Sales"), when the sheet belongs to the file containing the macro. The fix is confirmed when the line runs without error and targets the intended sheet.

3. Verify the workbook is open and named correctly

For a reference such as Workbooks("Report.xlsx"), check the open workbook's displayed name in Excel. The name must match, including the extension when one is used in the code. In the VBA editor, press Ctrl+G to open the Immediate window and run ? Workbooks.Count; list open workbook names with a short loop or inspect the Excel window. If the file is not open, open it first or change the macro to open the correct path. Confirm the expected workbook object is found before accessing its sheets.

4. Check array bounds before indexing

At the highlighted array access, inspect where the array is declared or populated. Use LBound(arrayName) and UBound(arrayName) to find its valid index range; do not assume every array starts at zero or one. For a loop, use those bounds instead of a hard-coded range, and check that the array was initialized before reading it. If it is a dynamic array, confirm the code called ReDim before indexing. The fix is confirmed when the requested index falls within the reported bounds.

5. Correct loop limits for collections

For a collection such as Worksheets or Workbooks, check its Count and the loop's starting and ending values. A collection indexed from 1 through Count will fail if the loop goes one item too far. Use For i = 1 To Worksheets.Count when iterating through Excel worksheets, and avoid reusing a count captured before the collection changes. Run the loop again; it should visit each existing item without requesting an extra one.

6. Qualify references in Office automation

When Excel, Word, or Outlook is automated from another Office host, an unqualified reference may point to the active document or workbook rather than the one your code expects. Use an explicit object variable and qualify child objects, for example Set wb = Workbooks.Open(filePath) followed by wb.Worksheets("Data"). In Word, check the intended Documents collection item; in Outlook, confirm the item is in the folder or collection being searched. The fix is confirmed when the code consistently accesses the selected object, even if another document or workbook is active.

7. Check imported data and generated file names

Power Query refreshes, PDF exports, and other automation can change which sheets, rows, or files exist. If the macro expects an imported sheet or a PDF-derived output name, check the result after refresh or export before looking it up. Inspect the workbook's sheet tabs and the exact generated filename, then adjust the code or ensure the preceding step completed successfully. The reference should resolve after the expected output exists.

FAQ

How do I fix VBA error 9 subscript out of range in Excel?

Choose Debug and inspect the highlighted line. Compare the requested worksheet or workbook name with the actual open item, or check the index against the array or collection bounds. Correct the mismatch and run the macro again.

Why does my macro work on one computer but not another?

The workbook, worksheet, or generated file names may differ, or a required workbook may not be open on the other computer. Check the exact names and qualify references to the intended workbook. Also confirm that any imported data or output files were created before the macro accesses them.

Can a missing worksheet cause Run-time error 9?

Yes. Requesting a worksheet by a name that is not present in the referenced workbook raises this error. Check the tab name and the workbook object used by the code, including whether the sheet was renamed or never created.

Run the macro again after correcting the missing name, invalid index, or wrong object reference, and confirm it reaches the intended item. If the error remains, use Debug to identify the next failing line and inspect the actual names and bounds at that point.

Photo: Aleks Dorohovich aleksdorohovich / CC0 (Wikimedia Commons)

Leave a Comment

PDF Preview Broken After Windows Update: Fix

PDF Preview Broken After Windows Update: Fix

Fix pdf preview not working after windows update with safe checks for File Explorer, Outlook, PDF handlers, file permissions, Office workflows, and automation.

Mail Merge in New Outlook: What Works Instead

Mail Merge in New Outlook: What Works Instead

Mail merge not working with new Outlook? Learn why classic Outlook matters and use Word, Excel, PDF, or Power Automate alternatives to send documents today.

Google Sheets Keeps Scrolling Right: Frozen Panes

Google Sheets Keeps Scrolling Right: Frozen Panes

Google Sheets keeps scrolling to the right? Find out how frozen columns, hidden columns, browser zoom, and trackpad gestures cause it, then fix the view.

Google Sheets Keeps Changing Date Format: Locale Fix

Google Sheets Keeps Changing Date Format: Locale Fix

Google Sheets keeps changing date format? Fix spreadsheet locale, import settings, and formulas so dates stay consistent across Sheets, Excel, Power Query, and PDF exports.

Power Query Preview Is Evaluating Forever: Fix

Power Query Preview Is Evaluating Forever: Fix

Power query stuck on preview is evaluating? Find the cause, stop long-running previews, and fix Excel Power Query with practical checks you can do today.

Power Query Stuck on Loading Data: Privacy Levels

Power Query Stuck on Loading Data: Privacy Levels

Fix excel power query stuck on loading data by checking privacy levels, isolating slow sources, and reviewing refresh settings with these practical steps.

VBA Error 9 Subscript Out of Range: Debug Guide

VBA Error 9 Subscript Out of Range: Debug Guide

Fix vba error 9 subscript out of range in Excel and Office macros by checking workbook, worksheet, array, and collection references with practical tests.

PDF Error 108 startxref Missing: Repair Without Loss

PDF Error 108 startxref Missing: Repair Without Loss

Fix pdf error 108 startxref missing or invalid by checking the file source, repairing a copy, and restoring PDFs safely in Excel, Word, Outlook, and automation.

Excel Deletes Leading Zeros: 5 Fixes That Stick

Excel Deletes Leading Zeros: 5 Fixes That Stick

Excel removes leading zeros because it treats codes as numbers. Fix imports, formats, and formulas to keep IDs, ZIP codes, and account numbers intact in every workflow.

Excel Changes the Last Digit to 0: Precision Fix

Excel Changes the Last Digit to 0: Precision Fix

Excel keeps changing last digit to 0? Learn why 16-digit values lose precision and how to preserve IDs in Excel, Power Query, Word, Outlook, and PDFs.