ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Efficient Code to Copy a 1-D Array to a Worksheet Column (https://www.excelbanter.com/excel-programming/378875-re-efficient-code-copy-1-d-array-worksheet-column.html)

Tom Ogilvy

Efficient Code to Copy a 1-D Array to a Worksheet Column
 
I suspect that is the way. Just to add, however,


In VBA it would be

Range("A1:A10").Value = Application.Transpose(my1Darray)

so perhaps you can figure out how to do that in C#.

--
Regards,
Tom Ogilvy



"R.E." wrote in message
...
Microsoft Support explains how to copy an array to a range in the Article
(ID
306023) "How to transfer data to an Excel workbook by using Visual C# 2005
or
Visual C# .NET":
objRange = objSheet.get_Range("A2", m_objOpt);
objRange = objRange.get_Resize(100,3);
objRange.Value = objData;
Although the last line should be:
objRange.set_Value(Type.Missing, objData);

When using a 1D array to transfer data, set_Value assumes the 1D array is
a
row of data, even if the range specifies a single column of cells:
float[] aryFloats;
objRange = objSheet.get_Range("A2", Type.Missing);
objRange = objRange.get_Resize(aryFloats.Length, 1);
objRange.set_Value(Type.Missing, aryFloats);
How can I efficiently copy a large 1D array into a column of cells? Is
there a more efficient way than copying the data from the 1D array into a
2D
array?




R.E.

Efficient Code to Copy a 1-D Array to a Worksheet Column
 
1-D arrays do not have a transpose.
P.S. Copying the data from the 1D array into a 2D array does work, but is
adds efficiency due to the extra copy step. Using single-dimensioned,
zero-based arrays is recommended for best performance in general.

Tom Ogilvy

Efficient Code to Copy a 1-D Array to a Worksheet Column
 
< smile

--
Regards,
Tom Ogilvy


"R.E." wrote in message
...
1-D arrays do not have a transpose.
P.S. Copying the data from the 1D array into a 2D array does work, but is
adds efficiency due to the extra copy step. Using single-dimensioned,
zero-based arrays is recommended for best performance in general.




Jon Peltier

Efficient Code to Copy a 1-D Array to a Worksheet Column
 
Choose your inefficiency:

(a) Invoke WorksheetFunction.Transpose(MyArray) [I don't know how you would
do it in C#; that's your problem]

(b) Create and populate a 2D array from the 1D array.

If I were doing this in VBA I would try both ways with a timer to see which
performance hit was worse.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"R.E." wrote in message
...
Correction on my typo: Copying the data from the 1D array into a 2D array
does work, but adds inefficiency due to the extra copy step. I'd like a
more
efficient way. My app is going to be slow enough for the large arrays I
have, without adding an extra copy step.




Jon Peltier

Efficient Code to Copy a 1-D Array to a Worksheet Column
 
If I were doing this in VBA I would try both ways with a timer to see
which performance hit was worse.


.... and if it didn't matter much, I'd use WorksheetFunction.Transpose,
because I'm basically lazy.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"Jon Peltier" wrote in message
...
Choose your inefficiency:

(a) Invoke WorksheetFunction.Transpose(MyArray) [I don't know how you
would do it in C#; that's your problem]

(b) Create and populate a 2D array from the 1D array.

If I were doing this in VBA I would try both ways with a timer to see
which performance hit was worse.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"R.E." wrote in message
...
Correction on my typo: Copying the data from the 1D array into a 2D array
does work, but adds inefficiency due to the extra copy step. I'd like a
more
efficient way. My app is going to be slow enough for the large arrays I
have, without adding an extra copy step.







All times are GMT +1. The time now is 10:25 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com