Solved

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

Posted on 2011-03-08
2
781 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:MeritorWabco
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 125 total points
ID: 35070852
Should that not be:
MsgBox(Day1Info(1, 1).ToString)

Open in new window

0
 

Author Closing Comment

by:MeritorWabco
ID: 35070896
Yeah :) duh (bonking self in head)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now