How to dimension a zero-length array?
I have a function that will return an array of strings taken from an Excel worksheet based on an input parameter. Two of the three possible parameters create an array by looping through either the first row or first column of the worksheet (excluding cell "A1") and storing each cell's text as an array member until an empty cell is found. The other parameter uses the split function to take any text in cell "A1" and make an array using the newline character as a delimiter.
When everything contains text, no problems arise. However, if the "A1" cell of the worksheet is instead an empty cell, the split function returns an array with dimensions (0 to -1). If the row or column parameters are being used and the corresponding rows or columns in the worksheet are all empty cells, I want to duplicate that zero-length array to catch this occurrence in the calling routine.
I read this page on msdn which states:
Quote:
Empty Arrays. It is possible to use -1 to declare the upper bound of an array dimension. This signifies that the array is empty but not Nothing (Visual Basic). For more information, see Arrays in Visual Basic.
This lead me to another msdn page with this example:
Quote:
Zero-Length Arrays
An array with no elements is also called a zero-length array. A variable holding a zero-length array does not have the value Nothing. To create an array that has no elements, declare one of the array's dimensions to be -1, as shown in the following example.
Dim twoDimensionalStrings(-1, 3) As String
However, when I try to do this in my code, I get errors:
Code:
Sub temp()
Dim myArr1() As String
ReDim myArr1(-1) 'subscipt out of range
ReDim myArr1(0 To -1) 'subscript out of range
Dim myArr2(-1) As String 'range has no values
End Sub
Does anyone know what I'm doing wrong? I've considered just calling split on an empty string to duplicate the desired array, but I'd rather know why my other attemps aren't working. Any help is appreciated.
Re: How to dimension a zero-length array?
The MSDN links you have posted are for Net. For VBA and VB classic you can't declare an array like that. Using split to create one for you is fine in my opinion.