Excel VBA Subscript Out of Range
VBA “Subscript out of range” error occurs because the object we are trying to access does not exist. It is an error type in VBA codingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more, a “Run Time Error 9.” It is important to understand the concepts to write efficient code. It is even more important to understand the error of your VBA codeError Of Your VBA CodeVBA error handling refers to troubleshooting various kinds of errors encountered while working with VBA. read more to debug the code efficiently.
If you make a coding error and do not know what that error is when you are gone.
A doctor cannot give medicine to his patient without knowing what the disease is. Doctors and patients both know there is a disease (error), but it is more important to understand the disease (error) rather than give medicine to it. If you can understand the error perfectly, it is much easier to find the solution.
Similarly, this article will see one of the important errors we regularly encounter, i.e., the “Subscript out of range” error in Excel VBA.
You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA Subscript Out of Range (wallstreetmojo.com)
What is Subscript out of Range Error in Excel VBA?
For example, if you are referring to the sheet, not the workbook, then we get Run-time error ‘9’: “Subscript out of range.”
If you click on the “End” button, it will end the sub procedure. If you click on “Debug,” it will take you to the line of code where it encountered an error, and help will take you to the Microsoft website page.
Why Does Subscript Out of Range Error Occur?
As we said, as a doctor, it is important to find the deceased before thinking about the medicine. VBA “Subscript out of range” error occurs when the line of code does not read the object we entered.
For example, look at the below image. We have three sheets: Sheet1, Sheet2, and Sheet3.
Now in the code, we have written the code to select the sheet “Sales.”
Code:
Sub Macro2()
Sheets(“Sales”).Select
End Sub
If we run this code using the F5 key or manually, we will get the Run-time error ‘9’: “Subscript out of range.”
It is because we tried accessing the worksheet object “Sales,” which does not exist in the workbook. It is a run time error because it occurred while running the code.
Another common subscript error is when we refer to the workbook, which is not there. For example, look at the below code.
Sub Macro1()
Dim Wb As Workbook
Set Wb = Workbooks(“Salary Sheet.xlsx”)
End Sub
The above code says variable WB should be equal to the workbook “Salary Sheet.xlsx.” As of now, this workbook is not open on the computer. If we run this code manually or through the F5 key, we will get Run time error 9: “Subscript out of Range.“
It is due to the workbook we are referring to which is either not open or does not exist at all.
VBA Subscript Error in Arrays
When you declare the array as the dynamic array, and if you don’t use the word DIM or REDIM in VBAREDIM In VBAThe VBA Redim statement increases or decreases the storage space available to a variable or an array. If Preserve is used with this statement, a new array with a different size is created; otherwise, the current variable’s array size is changed.read more to define the length of an array, we usually get the VBA “Subscript out of range” error. For example, look at the below code.
Sub Macro3()
Dim MyArray() As Long
MyArray(1) = 25
End Sub
In the above, we have declared the variable as an array but have not assigned a start and ending point. Rather, we have assigned the first array the value of 25.
If we run this code using the F5 key or manually, we will get Run time error ‘9’: “Subscript out of Range.”
To fix this issue, we need to assign the length of an array by using the “ReDim” word.
Sub Macro3()
Dim MyArray() As Long ReDim MyArray(1 To 5)
MyArray(1) = 25
End Sub
This code does not give any errors.
How to Show Errors at the End of the VBA Code?
If you do not want to see the error while the code is up and running but needs an error list at the end, then you need to use the “On Error Resume” error handler. For example, look at the below code.
Sub Macro1()
Dim Wb As Workbook On Error Resume Next Set Wb = Workbooks(“Salary Sheet.xlsx”)
MsgBox Err.Description
End Sub
As we have seen, this code will throw Run time error 9: “Subscript out of range” in Excel VBA. But we must use the error handler On Error Resume Next in VBAOn Error Resume Next In VBAVBA On Error Resume Statement is an error-handling aspect used for ignoring the code line because of which the error occurred and continuing with the next line right after the code line with the error.read more while running the code. So, we will not get any error messages. Rather, the end message box shows me the error description like this.
You can download the Excel VBA Subscript Out of Range Template here:- VBA Subscript Out of Range Template
Recommended Articles
This article has been a guide to VBA Subscript Out of Range. Here, we learned the Error called “Subscript out of range” (Run-time error’9′) in Excel VBA, along with practical examples and a downloadable template. Below you can find some useful Excel VBA articles: –
- Declare Global Variables in VBAVBA AutoFillClear Contents in VBAExcel VBA UCase Function