Using Excel 2003 in, having trouble importing data

Posted on 2005-04-16
Last Modified: 2012-06-27

I'm having a problem with in programming to import data either from an array or using QueryTables to import a .csv file.  To preface this, I haven't had much experience with Excel in programming.  

When I go to importing the data (currently trying the easier of the two methods (array)) yields me a "object reference not set to an instance of an object" error.

Here is the code that matters:

        Dim excelApp As New Excel.Application
        Dim excelBooks As Excel.Workbooks = excelApp.Workbooks
        Dim excelBook As Excel.Workbook = excelBooks.Add
        Dim excelWorksheet As Excel.Worksheet
        excelApp.Visible = False
        Dim excelRange As Excel.Range
        excelWorksheet = excelApp.ActiveSheet()

        Dim ctr As Long = 0
        Dim args As Object

        For ctr = 0 To ArrayCtr
            args = HoldingArray(ctr)
            excelRange.Cells((ctr + 4), 2) = args  'This is where the program throws the error

To clarify: HoldingArray is the global array that holds the data, and ArrayCtr is the static counter that holds the toprange of the array.

Question by:icywindow
    LVL 28

    Expert Comment

    This is how I did it when working with Excel.  First you have to get the cell using the get_Range method of the worksheet:

    excelRange = excelWorksheet.get_Range("A2", "A2")

    Then to set the value, use the set_Value method of the excelRange:

    excelRange.set_Value(null, args)
    LVL 1

    Author Comment

    That doesn't seem to work when working with 2003 and the excel 11 com library (Excel 2003), for it seems that excel.worksheet.get_Range() doesn't exist.  Perhaps it's a library corruption all along?

    Thank you.
    LVL 1

    Author Comment

    Well, the libraries seem to be fine.  Should I go back to VB6 to make it work more easily, or should I use an earlier version of office?
    Neither get_range or set_value exist, but the range and value properties exist.  Should I use those?

    Thanks for your quick response
    LVL 28

    Accepted Solution

    I just checked my code and found that instead of set_value, I used Value2.

    As for get_range, it do exist in the worksheet.  Does your code look like this?

    Dim excelWorksheet As Excel.Worksheet
    excelRange = excelWorksheet.get_Range("A2", "A2")

    I am using Microsoft Office 11.0 Object Library.
    LVL 1

    Author Comment

    Well, it seems I found an easier way to do the same thing; that is using the worksheet.cells property, contextually

    Excel.Worksheet.Cells(RowIndex As Object, ColumnIndex As Object) = args as (Any Data Type)

    Either way, rafrancisco, I thank you for your response in a punctual fashion, you get the points.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now