Trying to replace Cells(RowIndex,ColumnIndex).Value() calls by row-wise references for performance, I permanently fail at referencing the result.
Starting with Excel Interop – Efficiency and performance, which contains the tip to use get_range, i.e.
//get values
object[,] objectArray = shtName.get_Range("A1:Z100").Value2;
iFace = Convert.ToInt32(objectArray[1,1]);
//set values
object[,] objectArray = new object[3,1] {{"A"}{"B"}{"C"}};
rngName.Value2 = objectArray;
I figured I cannot use get_Range since that method is not callable for me, says VB at run-time (not a visible member).
Now I came up with stuff like:
Dim Values As Object(,)
Values = Sheet.Range(Sheet.Cells(RowIndex, 1), Sheet.Cells(2, 17)).Value2
For Index As Integer = 0 To 16
MsgBox(Values(0, Index))
Next
However, referencing Values with two dimension indices always returns an “index was outside the bounds of the array” exception. Inspecting the array with the debugger shows a nice 2-dimensional array which should has 17 elements on the second dimension, so Value (0,0) should indeed be a valid reference — but it isn’t:
The debugger lets me inspect Value, I can also drill down to Value(0,0) and see the correct value, but re-evaluating just that element, i.e. inspecting “Value (0,0)” returns above message.
How can I reference what my Value2 call returns?
Perhaps something like this? Note that if you want the array to have the column as the first element you’d use change the line below as noted: