I don't like uninitialized VBA arrays, since it's necessary to check if array is initialized, each time prior using UBound()
or For Each
to avoid an exception, and there is no native VBA function to check it. That is why I initialize arrays, at least doing them empty with a = Array()
. This eliminates the need for extra check in most of cases, so there are no problems with 1d arrays.
For the same reason I tried to create an empty 2d array. It's not possible simply do ReDim a(0 To -1, 0 To 0)
, transpose 1d empty array or something similar. The only way I came across by chance, is to use MSForms.ComboBox
, assign empty array to .List
property and read it back. Here is the example, which works in Excel and Word, you need to insert UserForm
to VBA Project, place ComboBox
on it, and add the below code:
Private Sub ComboBox1_Change()
Dim a()
ComboBox1.List = Array()
a = ComboBox1.List
Debug.Print "1st dimension upper bound = " & UBound(a, 1)
Debug.Print "2nd dimension upper bound = " & UBound(a, 2)
End Sub
After combo change the output is:
1st dimension upper bound = -1
2nd dimension upper bound = 0
Actually it's really the empty 2d array in debug:
Is there more elegant way to create an empty 2d array, without using ComboBox
, or UserForm
controls in general?
#Declaring an empty 1D array. #Initialize the column. #Append the column to each row. # 2D array is created. #Print the two dimensional array. In this code, we will create a two-dimensional array using the function. We will take input from the user for row size and column size.
JavaScript suggests some methods of creating two-dimensional arrays. You can use the array constructor and the for loop to create a 2D array like this: Lieteral notation method can also be used to create 2D arrays: The Array.from () method will return an array object from any JavaScript object with the length property or an iterable object.
To create an empty array there is an inbuilt function in NumPy through which we can easily create an empty array. The function here we are talking about is the .empty () function. It accepts shape and data type as arguments. Then returns a new array of given shapes and data types.
#Print the two dimensional array. print (a) #Declaring an empty 1D array. #Declaring an empty 1D array. #Initialize the column. #Append the column to each row. # 2D array is created. #Print the two dimensional array.
Idk man - I think you stumbling onto this property was pretty wild.
I'd probably stop here and just do:
Function Empty2DArray() As Variant
With CreateObject("Forms.ComboBox.1")
.List = Array()
Empty2DArray = .List
End With
End Function
And use it like: a = Empty2DArray
You don't need to create the userform or combobox - you can just use CreateObject
.
But as others have said, it probably makes more sense to do error handling when checking whether or not your arrays are initialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With