This is an MCVE of a point which cropped up in a comment to an answer here today. The answer
was related to the task of transferring values from the 2nd dimension of a variant array
into a vertical column of a worksheet. The comment suggested using Excel's WorhSheetFunction.Transpose
to assign the variant's data into the destination cells. Fair enough.
The code below creates a one-dimensional array of values and then attampts to assign it to a column of cells in the worksheet. If you run it, you'll see that the result is
A1 = 1
A2 = 1
A3 = 1
This is the code
procedure AssignArray;
var
Excel,
WB,
Values : OleVariant;
begin
Excel := CreateOleObject('Excel.Application');
Excel.Visible := True;
WB := Excel.WorkBooks.Add;
try
Values := VarArrayCreate([0, 2], varVariant);
Values[0] := 1;
Values[1] := 2;
Values[2] := 3;
WB.ActiveSheet.Range('A1:A3') := Values;
ShowMessage('Here');
finally
WB := UnAssigned;
Excel := UnAssigned;
end;
end;
Iow you get the first value of the array in all 3 rows. Googling this result finds
Writing an array to a range. Only getting first value of array
in which all three answers say to use Transpose. Fine, but my question is, how to avoid the triplicated
first value problem in my case, where the source data is a single-dimensional array. Transposing a one-dimensional
array doesn't make sense ...
Values) to a two-dimensional range (Range['A1:A3']), the one-dimensional array needs to be promoted to a two-dimensional matrix. This happens all the time in pure mathematics: vectors are implicitly treated as column matrices most of the time, and if you sometime need a row matrix, you transpose. See, for instance, Wikipedia. And it so happens that...Range).