How can i write an entire dimension of a multidimensional array, in a single statement?
Edit 2: Clarification:
Simply want to update one slice (vertical or horizontal) of a VBA array, without looping/processing the entire array.
I'm assuming we need to convert a 2D to 1D, or 1D to 2D, array. Not sure that's necessary.
My example uses a worksheet range as a source, but don't be distracted by that. I'm not trying to update the worksheet-- trying to update an array. The solution i seek should not require worksheet read/write.
Edit 1: I've got a 2D array in VBA. It contains cell data pulled in from a sheet:
dim blockData
blockData = [a1:d10]
Now, i want to copy a different cell range into that array, overwriting just one row:
dim rowData
' just one row of data, but same width as 2D blockData
rowData = [a20:d20]
' load into array, overwriting row 3 with this new data.
' This is the one-liner i seek:
blockData(3,) = rowData
So, how can i write a whole 1D array to one D of a 2D array in a single statement, without looping?
Some ideas:
- I can get a single column from the array using the INDEX trick. Maybe that can be exploited somehow: colArray = WorksheetFunction.Index(rangeArray, 0, 1) colArray (1,2)="New cell value"
- Use Transpose(colArray), but that has not helped so far.
- Use Redim Preserve to somehow combine the arrays, but not sure how that can help.
- Use VarPtr to write my changes directory to the original array, but not sure that can be done without looping.
- It's easy to copy an entire array to another variable in one statement. Maybe that can be exploited somehow:
Dim a1, a2
a1=array(1,2,3)
a2=a1
thx