.netvb.netexcelinterop

How to debug an exception when trying iterate Excel range.value like array?


I tried the following VB.Net code:

Dim data As Object
Dim range As Excel.Range
range = WB.Sheets("mysheet").Range("A1:C5")
data = range.Value   ' Here WB is the Workbook object
' Until here its working fine.
' During Debug: I can see data with values for [ (0,0), (0,1), (0,2), (1,0), .. ]
' Now following line gives an exception !!
Dim val = data(0,0)  ' Index out of range exception.

How can I iterate the RangeValueDataType object like an array ?

Reference: http://msdn.microsoft.com/en-us/library/bb238606(v=office.12).aspx


Solution

  • AFIAK Excel Range data is array with 1 base index, i.e. first item will be data(1, 1).