ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to set values to a range vertically (https://www.excelbanter.com/excel-programming/414169-how-set-values-range-vertically.html)

-kve-

How to set values to a range vertically
 
Hi,

I am writing a C# program that interacts with Excel.

When I have a range in Excel that contains a few "horizontal" cells (e.g.
"A1" to "D1"), I can fill the range with values from an array:

Microsoft.Office.Interop.Excel.Range rangeSeries = wsData.get_Range("A1",
"D1");
range.set_Value(missing, array);

But when I try to do the same thing, but with a range with vertical cells,
all the cells in the range get the value of the first array field.

Anyone knows how this can be done?

Charlie

How to set values to a range vertically
 
I can't answer about C# but in VBA you need to dimension the array as Row X
Column to fit your range horizontally or vertically. Here's an example (I
always use 1-based arrays)

Option Base 1

Sub test()

Dim a1(1, 4)
Dim a2(4, 1)

a1(1, 1) = "A"
a1(1, 2) = "B"
a1(1, 3) = "C"
a1(1, 4) = "D"

a2(1, 1) = "E"
a2(2, 1) = "F"
a2(3, 1) = "G"
a2(4, 1) = "H"

Range("a1:d1") = a1
Range("a2:a5") = a2

End Sub


"-kve-" wrote:

Hi,

I am writing a C# program that interacts with Excel.

When I have a range in Excel that contains a few "horizontal" cells (e.g.
"A1" to "D1"), I can fill the range with values from an array:

Microsoft.Office.Interop.Excel.Range rangeSeries = wsData.get_Range("A1",
"D1");
range.set_Value(missing, array);

But when I try to do the same thing, but with a range with vertical cells,
all the cells in the range get the value of the first array field.

Anyone knows how this can be done?



All times are GMT +1. The time now is 01:39 AM.

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