Redim Preserve Mystery - Excel Can't Redim Preserve array with 1

apaivas

New Member
Joined
Nov 18, 2013
Messages
5
Team,
After this last upgrade Microsoft pushed this past weekend, many of my workbooks stopped working when I go from Redim vArray(0 to 0) to Redim Preserve vArray(1 to 1) - Apparently 1 is skipped.
Simple Code 1 - Not working:
Code:
Private Sub ArrayTest1()
    Dim vArray As Variant
    ReDim vArray(0 To 0)
    ReDim Preserve vArray(1 To 1)
    MsgBox (UBound(vArray)) ' Returns 0?????
    ReDim Preserve vArray(1 To 2)
    MsgBox (UBound(vArray)) ' Returns 2
    ReDim Preserve vArray(1 To 3)
    MsgBox (UBound(vArray)) ' Returns 3
 End Sub
------
Simple Code 2 - Ugly Workaround that works
Code:
Option Explicit

Private Sub ArrayTest2()
    Dim vArray As Variant
    ReDim vArray(-1 To 0) 'Crazy but it works!!!!
    ReDim Preserve vArray(1 To 1)
    MsgBox (UBound(vArray)) ' Returns 1 - Check!
    ReDim Preserve vArray(1 To 2)
    MsgBox (UBound(vArray)) ' Returns 2 - Check!
    ReDim Preserve vArray(1 To 3)
    MsgBox (UBound(vArray)) ' Returns 3 - Check!
 End Sub
Can anybody tell what happened, I can spend a ton of time implementing the workaround, but what did Microsoft do? Why??
Thanks!!
 
Last edited by a moderator:
Team,

Apparently what I found "ugly" is the best practice for creating an empty (Zero-Length) array.

Thank to all for the replies, below is the link explaining, from the moment I read this article I decided that my workaround Dim myArray(-1 to 0) was no longer a work around.

Arrays in Visual Basic
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Just FYI, that article relates to .Net languages and some of it won't apply to VBA.
 
Upvote 0
I also use Redim myArray(-1 To -1) as a "empty array" flag (similar to the return from Split).

But when I fill my first element, I Redim myArray(0 to 0) and Redim Preserve only on filled arrays.

Another approach that I sometimes use invokes a Pointer variable. Testing the Pointer variable to see if the array is empty.

Code:
Dim myArray() as String
Dim Pointer as Long
Dim myArray(0 to 0)

Pointer = 0

'...
If Ubound(myArray) < Pointer Then
    Redim Preserve myArray(0 to 2 * Pointer)
End If

myArray(Pointer) = "new value"
Pointer = Pointer + 1


'....
If 0 < Pointer then
    Redim Preserve myArray(0 to Pointer -1)
End If
 
Upvote 0
Mike - This is cool! Thanks for the hint!!!

Rory - You are correct, that article relates to .NET, but I had to use something to alleviate my bad feeling when I used the Dim myArray(-1 to 0) and that article did the job I am no longer shamefaced.

Thanks again,

Suny
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,925
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top