Subscript Out of Range: How Do You Fix It?
When running a VBA procedure, you might receive this error message
“Run-time error ‘9’, Subscript out of range”. In this article, we will talk about the possible reasons for the occurrence of this error and the ways to fix it.
According to our recent review, there are four categories that can trigger this error:
- Referring to a nonexistent element
- Wrong collection member
- Unidentified element
- Shorthand script
We are going to take a look at each of these categories and show you how to generate this error and share the best way to fix it.
Referring to a nonexistent element
In some cases, you might have referenced an element (a workbook, worksheet, array, etc.) in the code that doesn’t exist. It is also possible that the subscript is either smaller or larger than the range of possible subscripts.
You referred to a nonexistent Worksheet
To generate this error, open the VBA editor and write the following code to select the sheet “customers” in Module1.
Sub Test1() Sheets("customers").Select End Sub
If you run the code, you will have the error message.
The reason for this error is the fact that there is no sheet named “customers” in the workbook. Our workbook has three sheets named respectively Sheet1, Sheet2 and Sheet3.
To fix the error, create a worksheet named “customers” or rename any of the three existing sheets and run the macro again.
You referred to a Workbook that is not opened on your computer
If you run the code below, you will have the error code because you will be referring to a workbook that is not opened on your computer.
Sub Macro3( B ) Dim Wbk As Workbook Set Wbk = Workbooks("Employees.xls") End Sub
Wrong collection member
The subscript may be larger or smaller than the range of possible subscripts.
To generate this error, run the following code:
Sub Macro( ) Dim PriceArray(5 To 10 ) As Integer PriceArray(3) = 20 End Sub
Sub Macro( ) Dim PriceArray(5 To 10 ) As Integer PriceArray(15) = 20 End Sub
The error will be triggered after running any of the two macros above because the PriceArray has been dimensioned from 5 to 10. But the code is referring to a subscript of index 3 (lower than 5) and 15 for the second macro (larger than 10). Elements of arrays and members of collections can only be accessed within their defined ranges.
That is why it is important to check the declaration of the array to verify it’s upper and lower bounds. It is also advisable to use the UBound and LBound functions to condition array accesses if you are working with arrays that are redimensioned.
You declared an array but didn’t specify the number of elements
The following code will generate the error:
Sub Macro2( ) Dim PriceArray( ) As Integer PriceArray(5) = 20 End Sub
The error occurs because when declaring the array named PriceArray, the number of elements was not specified. In fact, Visual Basic does not implicitly dimension unspecified array range as 0 to 10.
To avoid the error, you must explicitly specify the number of elements in the array as in the code below:
Sub Macro3( ) Dim PriceArray(0 To 10 ) As Integer PriceArray(5) = 20
You might have used a shorthand form of the subscript and it specifies an invalid element. Therefore, it is recommended that you use a valid key name and index for the collection.
For example [A10] is the shorthand for ActiveSheet.Range(A10). This means that instead of writing ActiveSheet.Range(A10).Select you can just write [A1].Select
If you don’t have a good knowledge of shorthand scripting, avoid using it because it can generate many errors, not just the “Subscript is out of range” error.
The aim of this article was to explain the possible reasons (the list is not exhaustive) for the occurrence of the “Subscript is out of range” error. Through a step by step tutorial, you have been able to fix the error. We hope it has been useful.