Link to home
Start Free TrialLog in
Avatar of MeritorWabco
MeritorWabco

asked on

Index was outside of bounds of array when reading in excel range to VB.NET array

For some reason I am getting an index out of range exception and I am not sure why.  I am reading in an excel worksheet that has 5 rows and 4 columns.  The value in the first cell (A1) is 1.

When I am debugging I can see the value in my array but when I try to display the value in the message box I get the 'Index was outside the bounds of the array.' exception.

Any ideas?

 
Dim objApp As New Excel.Application
        Dim objBook As Excel.Workbook
        Dim objSheet As Excel.Worksheet
        Try
            objBook = objApp.Workbooks.Open(Application.StartupPath & "\Book1.xlsx")
            objSheet = objBook.Worksheets(1)

            Dim range As Excel.Range
            Dim usedRows As Integer = objSheet.UsedRange.Rows.Count
            Dim usedColumns As Integer = objSheet.UsedRange.Columns.Count
            range = objSheet.Range("A1").Resize(usedRows, usedColumns)

            Dim Day1Info(,) As Object
            Day1Info = range.Value
            MsgBox(Day1Info(0, 0).ToString)

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            objApp.Quit()
        End Try

Open in new window

Book1.xlsx
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MeritorWabco
MeritorWabco

ASKER

Yeah :) duh (bonking self in head)