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

    Import All Excel Workbooks in Folder as Linked Tables

    I have a few dozen Excel workbooks in a folder, and I'd like to import all sheets in each workbook to Access as linked tables. I have the following macro that creates linked tables from every sheet within a specific workbook, but I'd like to apply this to all workbooks in a folder.

    Could someone help me modify this code?


    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\etc..."
    
    
        Set xl = New Excel.Application
        xl.Visible = True
        Set wkb = xl.Workbooks.Open(strPathFile)
        With wkb
            For Each sht In .Worksheets
                
                Dim dataRange As String
                dataRange = Replace(sht.Range("A1").CurrentRegion.Address, "$", "")
    
    
                DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, sht.Name, strPathFile, True, sht.Name & "!" & dataRange
                
            Next
        End With
    
    
    End Sub


  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    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
    Thanks, that helped.

    One more thing: How would I close the Excel workbook after opening it and exporting to Access? I tried "Workbooks(filename).Close False", but got a subscript out of range error.

    Code:
        With wkb
            For Each sht In .Worksheets
                
                Dim dataRange As String
                dataRange = Replace(sht.Range("A1").CurrentRegion.Address, "$", "")
    
    
                DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel9, sht.Name, filename, True, sht.Name & "!" & dataRange
                
                Next
        End With
        Workbooks(filename).Close False

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    I've never used TransferSpreadsheet with an Excel object. Didn't know that was possible.

    Maybe:

    wkb.Close False
    Set wkb = Nothing
    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.

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

Similar Threads

  1. Replies: 6
    Last Post: 01-05-2014, 11:43 PM
  2. Combining Many Linked Excel Tables Into One
    By NateL in forum Import/Export Data
    Replies: 3
    Last Post: 12-04-2013, 02:24 PM
  3. Replies: 5
    Last Post: 01-29-2013, 06:00 PM
  4. Replies: 1
    Last Post: 01-09-2013, 04:11 PM
  5. Replies: 6
    Last Post: 05-16-2012, 12:43 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