VBA Runtime Error 381 occurs when your code attempts to access an element of a collection or an array using an index that does not exist. To fix this, you must ensure the index number is within the valid range of the object—usually by verifying the count of the collection before accessing a specific item or by adjusting the array bounds.
What You'll Need
- A computer with Microsoft Office installed (Excel, Access, Word, etc.).
- The VBA Editor (accessible via Alt + F11).
- The specific macro or module that is triggering the error.
Step-by-Step
- Trigger the Error: Run your macro until the "Runtime Error 381: Invalid property array index" dialog box appears.
- Locate the Faulty Line: Click the Debug button in the error dialog. VBA will highlight the specific line of code causing the crash in yellow.
- Identify the Index Variable: Look for any parentheses
()on that line. Inside these parentheses will be the index (e.g.,Collection(i)orMyArray(x)). The variable inside these parentheses is what is causing the error. - Inspect the Current Value: Hover your mouse cursor over the index variable (like
iorx) while the code is paused. A tooltip will appear showing the current numeric value of that variable. - Check the Collection Size: To see why that value is invalid, go to the Immediate Window (press Ctrl + G if it isn't visible). Type the following and press Enter:
? YourCollectionName.Count(ReplaceYourCollectionNamewith the actual name of your object).
If the count is 5 and your index variable is 6, you have found the cause. - Implement a Boundary Check: To prevent the error from recurring, wrap the problematic line in an
Ifstatement to ensure the index is valid. For example:If i > 0 And i <= YourCollectionName.Count Then' Your original code hereEnd If - Adjust Loop Limits: If the error occurred during a
Forloop, check your loop parameters. Ensure you are not looping from1 To Count + 1. Change the loop toFor i = 1 To YourCollectionName.Count. - Reset and Test: Click the Reset button (the square stop icon) in the VBA toolbar. Run the macro again to verify the fix.
Common Mistakes
- Assuming 0-Based Indexing: Many VBA collections (like Sheets or Workbooks) are 1-based, meaning the first item is 1. If you start a loop at 0, you will trigger Error 381.
- Hard-coding Index Numbers: Using a fixed number like
Collection(5)is risky. If the data source changes and only 4 items exist, the code will crash. Always use dynamic counts. - Ignoring Empty Collections: Attempting to access the first element of a collection that contains zero items will immediately trigger this error.
- Confusing Arrays with Collections: While both use parentheses, arrays may have different lower bounds (defined by
Option BaseorDim Array(1 To 10)), whereas collections almost always start at 1.
Tips
- Use On Error Resume Next Sparingly: While you can bypass the error using
On Error Resume Next, this is generally discouraged because it hides the problem rather than fixing it, which can lead to incorrect data processing. - Use LBound and UBound: When working with arrays, always use
LBound(ArrayName)andUBound(ArrayName)in your loops instead of hard-coded numbers to ensure you stay within the valid range. - Debug.Print: Use
Debug.Print iinside your loops to print the index to the Immediate Window. This helps you see exactly which number triggers the crash during long processes.
Troubleshooting
If the steps above do not resolve the issue, consider these scenarios:
The index seems correct but still fails: Check if the collection is being modified (items being deleted) inside the loop. If you delete item 2 while looping from 1 to 10, the total count shrinks, and the loop will eventually hit an index that no longer exists. To fix this, loop backwards: For i = YourCollectionName.Count To 1 Step -1.
The error occurs on a UserForm control: If you are accessing a control array or a specific property of a control, ensure the control is actually loaded and initialized before the code attempts to reference its index.
The error is intermittent: This usually suggests that the data being imported or processed varies in size. Ensure you are recalculating the .Count or UBound every time the macro runs, rather than relying on a saved value from a previous session.
Frequently Asked Questions
Is Error 381 the same as "Subscript out of range"?
Yes, they are functionally very similar. Both indicate that you are asking VBA for an item at a position that does not exist within the specified array or collection.
Why does my loop start at 1 instead of 0?
In VBA, most built-in collections (like the Worksheets collection) are 1-indexed. However, standard arrays can be 0-indexed. You should check the specific object documentation or use LBound to be sure.
Can this error be caused by a corrupted Excel file?
It is unlikely. Error 381 is almost always a logic error in the code regarding how it handles lists or arrays, not a result of file corruption.
How do I find the total number of items in my array?
Use the UBound function. For example, total = UBound(myArray) will give you the highest available index in that array.
Will adding "Option Base 1" at the top of my module fix this?
Only for arrays declared without explicit bounds (e.g., Dim arr(10)). It will not change the indexing of built-in VBA collections or arrays that have explicitly defined bounds like Dim arr(0 To 10).