Context:
I am reading in a range of dimensions (1 To n, 1 To 2) in one subroutine and writing this array as a string to a cell in a table with format "{"D1","Value1";"D2","Value2";..."Dn","Valuen"}".
In another subroutine I am reading the String generated by the first Sub and converting it back to a 2D array using the Evaluate function. This works fine except for the case where there is only 1 dimension, i.e. {"D1","Value1"}. This evaluates to a Horizontal 1D Array, not a Horizontal 2D Array.
Question:
How do I create a Horizontal 2D Array in VBA? i.e. Array(1 To 1, 1 To n)
What I have tried:
(see code below)
Array function creates a 1D Horizontal Array (arr0)
Evaluate function of string creates a 1D Horizontal Array (arr1)
Application.Transpose converts a 1D H Array to a 2D Vertical Array (arr2)
Transpose of the transpose converts back to a 1D H Array (arr3, same as arr1)
Redim Preserve to add a second dimensions yields an Error (arr1)
Redim Preserve to change bounds of transpose array yields an error bc you can only Redim the last dimension in a VBA array (arr2)
ReDim Preserve to extend outside dimension of the transpose arr works (arr2), but then the Re-Transpose is correct for the first row (arr4) but I cannot Redim the inner dimension to remove the Empty "row"
Writing to a range and reading back to an array works (arr5) but is messy for this sheet that will be used by other people (I don't want to overwrite anywhere they may have entered data)
Option Explicit Option Base 1
Public Sub Test() Dim dims, arr0, arr1, arr2, arr3, arr4, arr5 ' Option 1 --------------------------------- arr0 = Array("D", "28") ' arr0 = Array(1 to 2) As Variant ' arr0(1) = "D" As String ' arr0(2) = "28" As String ' Option 2 --------------------------------- dims = "{""D"",""28""}" ' dims = "{"D","28"}" As String arr1 = Evaluate(dims) ' arr1 = Array(1 to 2) As Variant ' arr1(1) = "D" As String ' arr1(2) = "28" As String ' Option 3 --------------------------------- arr2 = Application.Transpose(arr1) ' arr2 = Array(1 to 2, 1 to 1) As Variant ' arr2(1,1) = "D" As String ' arr2(2,1) = "28" As String ' Option 4 --------------------------------- arr3 = Application.Transpose(arr2) ' arr3 = Array(1 to 2) As Variant ' arr3(1) = "D" As String ' arr3(2) = "28" As String ' Option 5 --------------------------------- ReDim Preserve arr1(1 To 2, 1 To 2) ' Run-time error '9': Subscript out of range ' Option 6 --------------------------------- ReDim Preserve arr2(1 To 1, 1 To 2) ' Run-time error '9': Subscript out of range ' Option 7 --------------------------------- ReDim Preserve arr2(1 To 2, 1 To 2) ' arr2 = Array(1 to 2, 1 to 2) As Variant ' arr2(1,1) = "D" As String ' arr2(1,2) = Empty As Empty ' arr2(2,1) = "28" As String ' arr2(2,2) = Empty As Empty arr4 = Application.Transpose(arr2) ' arr4 = Array(1 to 2, 1 to 2) As Variant ' arr4(1,1) = "D" As String ' arr4(1,2) = "28" As String ' arr4(2,1) = Empty As Empty ' arr4(2,2) = Empty As Empty ReDim Preserve arr4(1 To 1, 1 To 2) ' Run-time error '9': Subscript out of range ' Option 8 --------------------------------- [A1].Resize(1, UBound(arr0) - LBound(arr0) + 1) = arr0 ' [A1:B1] = {"D","28"} As Range arr5 = [A1].Resize(1, UBound(arr0) - LBound(arr0) + 1).Value2 ' arr5 = Array(1 to 1, 1 to 2) As Variant ' arr5(1,1) = "D" As String ' arr4(1,2) = 28 As Double Stop End Sub
Conclusion:
All the answers I could think of all involve writing a separate function to test for the 1D array case and then:
- Write to a range and read back into 2D H Array (I could make this safe by adding a new worksheet, writing array, reading range, then deleting that worksheet)
- Use Option (7) above and Application.Index(arr4, 1, 0) to slice first row of array.
- Reinvent the "Evaluate" function to parse the array string, ReDim the output array, and loop thru to fill values.
All seem inelegant to me. Does anyone have a better solution or advice on how best to proceed?