I have a couple of basic questions about using arrays in VBA, which I have not found the answer to.
Firstly, is there a way to specify a one dimensional row vector in Excel VBA?
I am new to using arrays in Excel VBA, and have been defining row and column vectors as two dimensional arrays, as in the examples below
'Define a Local Row Vector ReDim aArray(1 to iCntRows, 1 to 1) 'Define a Local Column Vector ReDim aArray(1 to 1, 1 to iCntCols)
So secondly, is this best practice? Should I be using a one dimensional array instead?
Where you intend to copy the array to a range, like
Range(somerange) = aArray
Then , yes, use 2d arrays
You have to use the transpose function to convert from a 2d array.
new_array = Application.Transpose(Sheet1.Range("A2:A100").Value)
where new_array is a 1d array.
Sub tt() Const SZ As Long = 1000 Dim arr(), x ReDim arr(1 To SZ) For x = 1 To SZ arr(x) = x Next x 'add to sheet (as a row) Sheet1.Range("a1").Resize(1, SZ).Value = arr 'add to sheet (as a column) 'note there is an upper limit when using transpose of around 65k Sheet1.Range("a5").Resize(SZ, 1).Value = Application.Transpose(arr) End Sub