I want to create a VBA array that has zero elements but is for-loopable.
You can look at the code below to see what I mean:
Sub testloopemptyarr()
Dim spl() As String, v As Variant, count As Integer
spl = Split(Empty, ",") 'lbound=0, ubound=-1, zero elements
'ReDim spl(-1 To -1) ' one element
'ReDim spl(-1) ' does not compile: subscript out of range
'ReDim spl(-1 To 0) ' two elements
'ReDim spl(0 To -1) ' does not compile: subscript out of range
For Each v In spl
count = count + 1
Next
MsgBox count
End Sub
The msgbox will pop 0 in this case, because splitting an empty string will return a zero element array. No errors thrown when the for-loop is encountered, implying that the array is an allocated array.
If you test it, you can find out that, after Split() is called, lbound(spl) is 0, ubound(spl) is -1
But it is illegal to do ReDim spl(0 To -1)
(try uncomment the line and run)
So my question is:
How do I create an array that has the same behavior with the one produced by the Split() function?