Results 1 to 5 of 5
  1. #1
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108

    Import All Sheets from Excel to Access

    Hi Guys,



    I'm have an Excel workbook (2013) with two dozen sheets that I'd like to import to Access. The code below is failing when it reaches the "Do.Cmd ..." and it's showing a type mismatch error. Any idea how to correct it?



    Code:
    Sub ImportAllSheets()
    
    
    
    
    Dim wkb As Excel.Workbook
     Dim sht As Excel.WorkSheet
     Dim xl As Excel.Application
    
    
        Set xl = New Excel.Application
        xl.Visible = True
        Set wkb = xl.Workbooks.Open("C:\Users\docs\file.xlsm")
        With wkb
            For Each sht In .Worksheets
                DoCmd.TransferSpreadsheet sht.Name, HasFieldNames:=True
            Next
        End With
    
    
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,957
    AFAIK, can't use TransferSpreadsheet on a VBA object.
    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.

  3. #3
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108
    So, I modified the code a bit and was able to make it work...sort of. It's taking the first sheet and importing the same data over and over again with the name of the workbook's worksheet.

    Code:
    Sub ImportAllSheets()
    
    
    
    
    Dim wkb As Excel.Workbook
     Dim sht As Excel.WorkSheet
     Dim xl As Excel.Application
     
     Dim strPathFile As String
     strPathFile = "C:\Users\docs\file.xlsm"
    
    
        Set xl = New Excel.Application
        xl.Visible = True
        Set wkb = xl.Workbooks.Open("C:\Users\docs\file.xlsm")
        With wkb
            For Each sht In .Worksheets
                'DoCmd.TransferSpreadsheet sht.Name, HasFieldNames:=True
                DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, sht.Name, strPathFile, True
            Next
        End With
    
    
    End Sub

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,957
    You are using sht.Name in the TableName argument. That has nothing to do with data source. Need to specify Range argument.

    Per MS: A string expression that's a valid range of cells or the name of a range in the spreadsheet. This argument applies only to importing. Leave this argument blank to import the entire spreadsheet. When you export to a spreadsheet, you must leave this argument blank. If you enter a range, the export will fail.

    sht.Name & "!A1:some cell reference"
    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.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You might check out Ken Snell's site for Import and Export examples (VBA code)


    Ken's ACCESS Examples and Information for Working With EXCEL Workbook Files

    http://www.accessmvp.com/KDSnell/EXCEL_MainPage.htm

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

Similar Threads

  1. Replies: 5
    Last Post: 08-31-2012, 02:59 PM
  2. Replies: 1
    Last Post: 08-25-2012, 06:11 PM
  3. Replies: 2
    Last Post: 08-14-2012, 04:24 AM
  4. Import excel sheets to access.
    By calexandru in forum Import/Export Data
    Replies: 0
    Last Post: 08-19-2009, 09:44 AM
  5. Replies: 0
    Last Post: 04-29-2009, 04:27 PM

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