Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-08
2
Medium Priority
?
811 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

721 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