79765835

Date: 2025-09-16 06:24:10
Score: 2.5
Natty:
Report link

What's Going On?

When you declare Dim Arr(0) As Long, you're creating a static (fixed-size) array. Then you pass it to DoSomethingWithArray as a Variant using ByRef. Inside that procedure, you overwrite ArrayArg with a new array (localArr), which causes VBA to attempt to reassign the reference.

But here's the catch:

This is because VBA tries to reconcile the static array reference with the new dynamic array assignment, and in doing so, it essentially resets the original array.

How to fix it?

If you want predictable behavior, use a dynamic array instead:

Sub TestFixedSizeArrayAsByRefVariantArgument()
    Dim Arr As Variant
    ReDim Arr(0) As Long
    Arr(0) = 99
    DoSomethingWithArray Arr
    Debug.Print Arr(0) ' Outputs -1 as expected
End Sub

This works because Arr is a Variant holding a dynamic array, so reassignment inside the procedure behaves as expected.

Reasons:
  • RegEx Blacklisted phrase (1.5): How to fix it?
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Starts with a question (0.5): What's
  • Low reputation (1):
Posted by: MDragon