This error occurs when you try to resize, add to, or remove elements from a VBA array that has been declared with a fixed size or is currently locked by a user object. The solution is to either declare the array using the ReDim statement with the Preserve keyword to keep existing data, or to use dynamic arrays and release the object reference before modifying the array.
What You'll Need
- Microsoft Excel, Access, or Word installed with the VBA Editor (VBE).
- A basic understanding of variable declaration and arrays in VBA.
- Access to the VBA code window (Alt+F11) where the error is occurring.
Step-by-Step
- Identify the Array Declaration. Locate the
Dimstatement for the array causing the error. Look for theAs VariantorTypekeyword immediately following the array name. If the size is specified inside the parentheses, such asDim MyArray(10) As String, the array is fixed-size. If the parentheses are empty, such asDim MyArray() As String, it is a dynamic array. - Switch to Dynamic Array Declaration. Change the declaration to use empty parentheses. For example, change
Dim MyArray(10) As StringtoDim MyArray() As String. This tells VBA to allocate memory later when theReDimstatement is encountered. - Locate the
ReDimStatement. Find the line of code that attempts to set the size of the array. This is usually where theReDimkeyword is used. If the code is missing and you are simply trying to use the array, insert aReDimstatement immediately after theDimstatement to initialize it with a starting size. - Apply the
PreserveKeyword (If Necessary). If you are resizing an array that already contains data and you want to keep those values, add thePreservekeyword to theReDimstatement. For example:ReDim Preserve MyArray(1 To 20). Note that you can only resize the last dimension of an array when usingPreserve. - Check for Object Locking. If the array is not fixed-size, the error may be caused by the object containing the array being locked. In Excel, ensure that the worksheet or workbook is not set to Read-Only mode. In Access, close any forms or reports bound to the table where the data is being processed. If you are using a UserForm, ensure you are not iterating over collections while the object is still in memory.
- Test the Code. Run the procedure again. The error should be resolved. If the array is still locked, ensure that no other code blocks are currently executing that might be holding a reference to the array or the parent object.
Common Mistakes
- Using Fixed-Size Arrays for Variable Data. A common mistake is declaring an array with a large fixed size like
Dim Data(1000)but only using a few elements. If the code attempts to write to an index beyond the used range or tries toReDimthe array later, the error occurs. It is safer to start with a small size and expand as needed. - Modifying Dimensions Other Than the Last One. When using the
Preservekeyword, VBA restricts you to changing only the last dimension of a multidimensional array. Attempting to change the first dimension while preserving data will result in a runtime error. - Forgetting to Initialize a Dynamic Array. If you declare an array as dynamic but never use the
ReDimstatement, VBA will throw an error when you try to assign a value to it, as the memory has not been allocated.
Tips
- Prefer Dynamic Arrays. Unless you know the exact size of the data set in advance, it is always better practice to declare arrays as dynamic. This gives you the flexibility to adjust the size during execution.
- Use Option Explicit. Enable Require Variable Declaration in the VBE options (Tools > Options > Editor). This forces you to declare all variables, preventing typos that might lead to array errors.
- Check for Loop Conflicts. If you have nested loops or a loop inside a function that returns an array, ensure that the loop index does not conflict with the array bounds. Sometimes, an infinite loop can cause the array to appear locked or cause memory issues that manifest as this error.
Troubleshooting
If the error persists after changing the array declaration, perform the following checks:
- Check for Immediate Scope Conflicts. Ensure that you are not declaring the same variable name in two different scopes (e.g., one in a Module and one inside a Function) where one might be shadowing the other. Rename one of the variables to resolve the conflict.
- Verify Object References. If the array is part of a Class Module or a UserForm, ensure that you are not trying to modify the array while the object is still being initialized or loaded. Add a check, such as
If Not Me.IsLoading Then, to ensure the code only runs after the object is fully ready. - Inspect the Call Stack. If you are unsure which code line is triggering the error, use the VBA Editor's Debug > Toggle Breakpoint feature on the line where the array is declared. Run the code and press F8 to step through the lines. Watch the Locals Window (View > Locals Window) to see the values of your variables at each step.
Frequently Asked Questions
Can I change the size of a fixed-size array?
No, you cannot change the size of a fixed-size array after it has been declared with a specific number of elements. You must either declare a new array, copy the data to the new array, or delete the old array and redefine it.
What does the Preserve keyword do?
The Preserve keyword ensures that any existing data inside the array is not lost when you resize it. Without it, VBA clears all the data in the array when you change its size.
Why do I get a runtime error 10 even if I use ReDim?
If you are using ReDim on a dynamic array and still get the error, the issue is likely not the array size itself but that the parent object is locked. For example, if you are looping through a collection of objects and one of those objects is currently being edited by another process or has a property being accessed, you may not be allowed to modify the array associated with that object.
Is it better to use Collections instead of Arrays?
Often, yes. Collections are dynamic by default and do not require ReDim. However, arrays are generally faster for processing large amounts of data and are required for certain types of matrix operations or API calls.
How do I clear a VBA array?
You can clear a dynamic array by re-declaring it with the ReDim statement and an empty set of parentheses, or by using the Erase statement if the array holds variables or user-defined types. Simply setting the array variable to Nothing or assigning a value like MyArray = Null often does not clear the memory used by the array elements.