Error 9, “Subscript out of range”, means the code asked for an item that isn’t in a collection or array. The usual cause is a sheet or workbook name that doesn’t match exactly. Worksheets("Sales") fails when the tab is called “Sales ” with a trailing space, or “Sales 2026”, and Workbooks("Report") can fail when the open file is Report.xlsx, so include the extension.
Numbers fail the same way. Worksheets(5) fails in a workbook with four sheets, and arr(11) fails when the array was declared arr(1 To 10). In loops, use UBound(arr) and Worksheets.Count as the upper limit instead of a fixed number.
A dynamic array declared with empty brackets, as in Dim arr() As String, has no elements until you size it. Run ReDim arr(1 To 10) before assigning arr(1), or the first assignment gives error 9.
To check that a sheet exists before using it, try to set it with errors switched off:
Dim ws As Worksheet
On Error Resume Next
Set ws = ThisWorkbook.Worksheets("Sales")
On Error GoTo 0
If ws Is Nothing Then MsgBox "No sheet called Sales"
In VBA, the “Subscript Out of Range” error is like trying to pick a book from a shelf that isn’t there! Let’s say you have a box with five apples and want to pick the seventh; you can’t, right? It’s the same here.
What is Subscript Out of Range (Run time: Error 9)
Subscript Out of Range Error (Run Time: Error 9) occurs when you refer to an item of a collection (like a worksheet or a workbook) that doesn’t exist, or to an element of an array that is outside the array’s range. In that case, VBA shows this error. As every code that you write is unique, so is the cause of the error.

In the following example, you have tried to activate “Sheet1”, which is an object. But as you can see, no worksheet exists in the workbook with the name “Sheet1” (instead, you have “Sheet2”), so VBA shows “Subscript Out of Range” to notify you that there’s something wrong with the code.

The same happens with small mistakes in the name, like an extra space at the end (“Sheet1 ” instead of “Sheet1”), and when you use an index number for a sheet that isn’t there, like Sheets(5) in a workbook that has only three sheets.
Subscript Out of Range
There could be one more situation when you face the “Subscript Out of Range” error: when you declare a dynamic array but forget to use the ReDim statement to define the length of the array.

Now in the above code, you have an array with the name “myArray” and to make it dynamic, we have initially left the array length blank. But before you add an item you need to redefine the array length using the ReDim statement.
And that’s the mistake we have made in the above code, so VBA has returned the “Subscript Out of Range” error.
Sub myMacro() Dim myArray() As Variant myArray(1) = "One" End Sub
Wrong Sheet Name in a Range Reference
The below code refers to a range on “Sheet1”. “ZZ100” itself is a valid cell, as Excel has columns up to XFD, but if the workbook has no sheet named “Sheet1”, the line with ThisWorkbook.Sheets(“Sheet1”) shows the “Subscript Out of Range” error. A cell address beyond the last column or row of the sheet, like “XFE1”, gives Run-time Error 1004 instead, not Error 9.

Sub InvalidRange() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Sheet1") Dim rng As Range Set rng = ws.Range("ZZ100") rng.Value = "Data" End Sub
Workbook Not Open or Wrong Workbook Name
The same error shows up with workbooks. When you refer to a workbook with Workbooks(“Name”), VBA looks for it in the list of workbooks that are open right now. If the workbook is closed, or the name has a typo, you get the “Subscript Out of Range” error.
Sub ActivateSalesWorkbook()
Workbooks("Sales.xlsx").Activate
End Sub
In the above code, if “Sales.xlsx” isn’t open, VBA shows Error 9. To fix it, open the workbook first (with Workbooks.Open and the full file path), and use the workbook’s full name with the extension, like “Sales.xlsx” instead of “Sales”. A workbook that is open in a separate Excel window (another instance of Excel) is not part of the list either, so VBA can’t find it.
Array Index Outside Its Bounds
Even when you have defined the length of an array, you get this error when you use an index number that is smaller or larger than the array’s range. Let’s say you have declared an array with five elements, from 1 to 5. Now, when you try to use the sixth element, VBA shows Error 9.
Sub ArrayOutOfBounds()
Dim myArray(1 To 5) As Variant
myArray(6) = "Six"
End Sub
One more thing to keep in mind: when you declare an array as Dim myArray(5), it starts from 0 and has six elements (0 to 5), unless you have Option Base 1 at the top of the module. So, myArray(0) works there, but myArray(6) gives the error.
How Do I Fix Subscript Out of Range in Excel?
1. Debug the Code
The best way to deal with this Subscript Out of Range error is to write effective code and make sure to debug the code that you have written (step by step). In the Visual Basic Editor, press F8 to run the code one line at a time.

When you run a code step by step, it is easy for you to know on which line of that code you have an error, as VBA will show you the error message for Error 9 and highlight that line in yellow when you click Debug.
2. Use an Error Handler
The other thing that you can do is to use an “Error Handler” to jump to a specific line when an error happens.
In the following code, we have written a line to activate the sheet, but before that, we have used the On Error GoTo statement to move to the error handler. In the error handler, you have a message box that shows you a message with the Err.Description that an error has occurred.

So, when you run this code and “Sheet1” is not in the workbook where you are trying to activate it, it will show you a message box just like below.

And if “Sheet1” is there, there won’t be any message at all, because the “Exit Sub” line ends the code before it reaches the error handler. Without that line, VBA runs the lines under “myError:” even when there’s no error.
Sub myMacro() Dim wks As Worksheet On Error GoTo myError Sheets("Sheet1").Activate Exit Sub myError: MsgBox "There's an error in the code: " & Err.Description & _ ". That means there's some problem with the sheet " & _ "that you want to activate" End Sub
3. Use Option Explicit
At the top of your module, enter Option Explicit to force VBA to declare all variables. It can help you avoid typing mistakes in variable names, which might cause unexpected behavior while running the code.
Note – You can also use an add-in like MZ-Tools to write VBA codes, which helps you while you write the code. You can review your code and use error handling, and it also gives you automatic correction suggestions.
4. Check If the Sheet Exists Before You Use It
Most of the time, this error comes from a sheet name that isn’t there. So, instead of jumping to an error handler, you can check the sheet first. The function below returns True if the sheet exists and False if it doesn’t. Add it to a standard module, along with the macro that uses it.
Function SheetExists(sheetName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = ThisWorkbook.Worksheets(sheetName)
On Error GoTo 0
SheetExists = Not ws Is Nothing
End Function
Sub ActivateSheetSafely()
If SheetExists("Sheet1") Then
Worksheets("Sheet1").Activate
Else
MsgBox "There's no sheet named Sheet1 in this workbook."
End If
End Sub
After that, the macro activates “Sheet1” only when it’s in the workbook, and shows a message when it’s not, without any error.
5. Use LBound and UBound with Arrays
When you loop through an array, don’t type the start and end numbers yourself. Use LBound and UBound, which return the lowest and the highest index of the array, so the loop never goes outside its range.
Sub LoopArraySafely()
Dim myArray(1 To 5) As Variant
Dim i As Long
For i = LBound(myArray) To UBound(myArray)
myArray(i) = i * 10
Next i
End Sub
And if you need a collection, like all the worksheets in a workbook, use a For Each loop instead of index numbers. It goes through the items that are there and never asks for one that isn’t.
Frequently Asked Questions
What does Run-time Error 9 mean in VBA?
It means your code asked for an item that isn’t there: a worksheet, workbook or other collection item that doesn’t exist, or an array element outside the array’s range.
Why do I get Error 9 when the workbook is open?
Check the name you are using. It must match the workbook’s name exactly, with the extension (like “Sales.xlsx”). Also, if the workbook is open in a separate instance of Excel, VBA can’t see it from your code’s instance.
Is Error 9 the same as Run-time Error 1004?
No. Error 9 is about an item or index that doesn’t exist, while Run-time Error 1004 is a general error that Excel returns when a method or property can’t do its job, like referring to a cell address beyond the last column or row of the sheet.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.