Results 1 to 6 of 6
  1. #1
    Mclaren is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164

    Importing data from excel:

    I am adapting code writen by, Crystal, to use with Access 2010 and Excel 2010. (The code is only an extract)



    Code:
    'written by Crystal
    'this was written for training purposes
    'please share freely but do not remove these lines
    'strive4peace2004@yahoo.ca
    'modified April 21, 2005
    I have the following code that gives an "Compile error: Variable not defined" error on the following line, with the word "xlDown" highlighted.

    I am unsure how to rectify this, any help would be greatly appreciated.

    Code:
    mNumRows = .Range("A1").End(xlDown).Row
    Code:
    Private Function ImportSheet(pTable As String) As Boolean
        On Error GoTo ImportSheet_error
        ImportSheet = False
       
        'Dim e As Excel.Application ' changed to the line below, as this code does not work in Access 2010
        Dim xlApp As Object
        Dim mFieldnames As String
        Dim s As String
        Dim mNumCols As Long
        Dim mNumRows As Long
        Dim i As Long
        Dim j As Long
        Dim mField As String
        
        On Error Resume Next
        Set xlApp = GetObject(, "Excel.Application")
       
        'If an instance of Excel was not open, then open it
        On Error GoTo ImportSheet_error
        If TypeName(xlApp) = "Nothing" Then
           Set xlApp = CreateObject("Excel.Application")
        End If
       
        xlApp.Workbooks.Open Me.ImportFile
        With xlApp.Sheets(Me.SheetNumber)
           'see how may rows there are
           mNumRows = .Range("A1").End(xlDown).Row
           'see how mamy columns there are
           mNumCols = .Range("A1").End(xlToRight).Column
           If mNumRows < (1 + Abs(Me.chkFirstRowFieldnames)) Then
              MsgBox "You don't have any rows in " & Me.SheetName & " to import", , "Aborting import..."
              GoTo ImportSheet_exit
           End If
           If mNumCols < (1) Then
              MsgBox "You don't have any columns in " & Me.SheetName & " to import", , "Aborting import..."
              GoTo ImportSheet_exit
           End If

  2. #2
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Do you have a reference set to Microsoft Excel xx.0 Object Library? ("xx" is the version number. For me, it is 11.0 ( A2K3))

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I tested code and the xlDown worked. Can't replicate the issue. Do you want to provide files for analysis? Follow instructions at bottom of my post.

    In 2010 the reference is 12.0. Excel should have automatically selected that reference when the VBA editor was opened, unless maybe the workbook was originally created with an earlier version.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Mclaren is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    Excel workbook is 2003. Extension is .xls

    Will check my references now

  5. #5
    Mclaren is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    Ok, checked my references and;

    The Excel 14.0 Object Library was NOT checked. I have now checked it.

    Now i get the following error:

    Error 1004

    Application-defined or object-defined error

  6. #6
    Mclaren is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2010
    Location
    Johannesburg
    Posts
    164
    I stand corrected, i had a goto to skip the problem area. so no more error. thanks guys. You helped a lot.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Issue with importing excel data
    By Jrbeene86 in forum Import/Export Data
    Replies: 0
    Last Post: 03-27-2012, 08:32 PM
  2. Importing Data from Excel to Access
    By dkatorza in forum Import/Export Data
    Replies: 3
    Last Post: 11-05-2011, 09:17 AM
  3. importing data from an excel file
    By slimjen in forum Access
    Replies: 3
    Last Post: 09-21-2011, 12:38 PM
  4. Importing ever-changing Excel data
    By jtf1972 in forum Database Design
    Replies: 7
    Last Post: 08-05-2011, 11:24 AM
  5. Importing Data From Excel
    By king_bowzow in forum Import/Export Data
    Replies: 1
    Last Post: 09-11-2009, 02:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums