When you encounter the VBA "Runtime Error 385: Need property 'Array Index'", it means your code is trying to access an array element using an invalid index—either the index is too large (out of bounds), a variable hasn't been properly initialized as an array, or the code assumes an array exists when it doesn't. This error blocks your macro from running, and fixing it requires identifying where the mismatch occurs. The fix almost always involves adjusting array dimensions, validating array references, or ensuring proper array declaration in your code.

What You'll Need

  • A working copy of Microsoft Excel with the VBA editor (Access it via Developer tab > Visual Basic or press Alt+F11)
  • Your VBA macro code (as a text file or in the Excel editor)
  • A basic understanding of array indexing and VBA data structures

Step-by-Step

  1. Identify the exact line of code where the error occurs. - Open the VBA editor and navigate to the module where your error occurs. - Right-click the error message in the Immediate Window (press Ctrl+G to open it, then type the error line number) to jump to the problematic line.
  2. Check array dimensions and ensure the index is valid. - Look for lines like: MyArray(5) = 10 or Dim MyArray(10) As Variant - If the index (5 or 10) exceeds the declared size (e.g., a 10-element array can only access up to index 10), the error will appear. - For dynamic arrays, ensure ReDim is called with the correct upper bound (e.g., `ReDim MyArray(1 To 10)` for 11 elements).
  3. Verify proper array declaration. - Common mistakes include:
    • Using Dim instead of Dim As Variant for variable arrays (VBA requires explicit type for dynamic arrays).
    • Trying to use array syntax on a non-array variable (e.g., `Dim MyValue As Integer` followed by `MyValue(1) = 1`).
    • Forgetting to initialize arrays before use (e.g., `Dim MyArray()`, then `MyArray(0) = 1` without `ReDim`).
    - Correct declarations: Dim MyArray(1 To 5) As Variant (1-based indexing) or Dim MyArray(10) As Variant (0-based indexing).
  4. Debug array access using error handling. - Wrap problematic code with error handling to isolate the issue: On Error Resume Next MyArray(10) = 100 ' Index out of bounds If Err.Number <> 0 Then MsgBox "Index " & Err.Number & " is invalid for array of size " & UBound(MyArray) End If On Error GoTo 0 - For dynamic arrays, check if `UBound(MyArray)` returns the expected value.
  5. Review loops and subroutines that reference arrays. - If your code loops through arrays (e.g., `For i = 0 To UBound(MyArray)`), ensure the loop variable matches the array size. - Example: A loop from `i = 1 To 10` will fail on a 9-element array.
  6. Test with a minimal example to isolate the issue. - Create a new VBA module and test a single array operation: Sub TestArray() Dim MyArray(1 To 3) As Variant MyArray(1) = 10 ' Works MyArray(4) = 20 ' Error End Sub - If this reproduces the error, the problem is in array initialization. If not, the issue lies elsewhere (e.g., a nested call or incorrect reference).

If That Did Not Work

If none of the above resolves the error, the issue might stem from: - A hidden dependency on another macro that modifies arrays unexpectedly. - A typo in the array name or reference (e.g., `MyArray` vs. `myArray`). - A corrupted VBA project file (try opening a fresh file and copying the problematic code). Try these advanced steps:
  1. Open a new workbook and paste your code into a new module. If the error persists, the issue is in variable scope or external references.
  2. Search your VBA project for the array name (Ctrl+Shift+F) to ensure no typos exist across multiple locations.
  3. Reopen Excel and restart your computer—sometimes cached references cause issues.

Common Mistakes

  • Using zero-based indexing incorrectly: VBA arrays are 0-indexed by default unless specified (e.g., `Dim MyArray(1 To 5)`). A code snippet like `MyArray(5) = 10` will fail if the array only has 4 elements (indices 0–3).
  • Forgetting to initialize dynamic arrays: Without `ReDim`, VBA allocates nothing, causing every access to trigger the error. Example: Dim MyArray() As Variant is invalid—use ReDim MyArray(1 To 10) instead.
  • Assuming `UBound()` returns the correct size: If your array is resized dynamically, `UBound()` might not reflect the latest state. Check manually or use error handling to verify bounds.
  • Using arrays in loops without bounds checks: A `For` loop like `For i = 1 To 5` will crash if the array has only 3 elements. Always use `UBound(arrayName)` to determine the limit.

Tips

  • Use the Immediate Window (Ctrl+G) to inspect array variables: ? UBound(MyArray) will display the last valid index of `MyArray`. If it shows a negative number, the array is uninitialized.
  • Leverage `Err.Number` and `Err.Description` to debug errors programmatically: If Err.Number = 385 Then MsgBox Err.Description helps identify why the error occurred.
  • Test with simple arrays to avoid scope issues: Start by declaring a 1D array with fixed size, then expand to 2D arrays if needed.
  • Avoid global variables for arrays in macros—they can cause unintended side effects across different modules.
  • Use `Array` function for testing: In the Immediate Window, type `? Array(1,2,3)` to verify array syntax. This mimics VBA's built-in array behavior.

Troubleshooting

  1. Restart Excel and VBA editor: Sometimes keyboard/mouse conflicts or corrupted references trigger this error. Close all instances of Excel and restart the editor.
  2. Check for macro dependencies: If your code calls other macros that modify arrays, ensure those macros are error-free. Use the Call Stack (Debug > Call Stack) to trace dependencies.
  3. Enable debugging mode: Set breakpoints on array-related lines (click the left gutter) and step through code (F8) to see where the index invalidation occurs.
  4. Update Excel: Older versions may have bugs in array handling. Update to the latest version of Excel if possible.
  5. Recreate the macro: If the error persists after testing a minimal example, the macro may be corrupted. Copy-paste the problematic code into a new module to eliminate hidden references.
  6. Contact Microsoft Support: If none of the above works and the issue involves a known bug (e.g., Excel 2019), file a support request with Microsoft. Provide the exact steps to reproduce the error.

Frequently Asked Questions

Why does my VBA array say "Need property 'Array Index'" when I know the index is valid?

This happens if the array is dynamically resized after declaration. Use `ReDim Preserve` to maintain existing elements during resizing, or check `UBound()` to confirm the index is still valid.

How can I convert a 1D array to 2D in VBA without triggering Error 385?

Use nested arrays: Dim My2DArray(1 To 2, 1 To 3) As Variant or reshape an existing array: ReDim Preserve MyArray(1 To 2, 1 To 3) As Variant

Is there a way to debug arrays without writing error handling?

Yes: In the Immediate Window, type: ? MyArray(UBound(MyArray)) to check if the last index exists. Alternatively, use the Watch window to monitor array sizes dynamically.

Why does "MyArray(5)" fail but "MyArray(4)" works?

VBA arrays are 0-indexed by default. If your array is declared as `Dim MyArray(4)` (indices 0–4), accessing `MyArray(5)` throws an error. To use 1-based indexing, declare it as `Dim MyArray(1 To 5)`.

Can I force an array to use 1-based indexing in Excel VBA?

Yes, declare it with `Dim MyArray(1 To 10) As Variant`. This skips index 0 entirely, making it 1-based. For 2D arrays, use `Dim MyArray(1 To 2, 1 To 3)`.

What should I do if my code uses `For i = 1 To 10` but the array only has 5 elements?

Change the loop to: For i = 1 To UBound(MyArray) or explicitly set the upper bound: For i = 1 To 5 (assuming the array is declared as `Dim MyArray(1 To 5)`).

Pro Tip: Use the "Array" Debugger

In the Immediate Window, type: ? Array(1,2,3,4,5) to see how VBA interprets array syntax interactively. This helps test array declarations before running your code.