Excel multiple sheet import for Access

daltonrogers_

New Member
Joined
Jun 5, 2013
Messages
4
Hi, I have found the code below on a posting. Link is below. Can you please make this work in Office 2010. I am wanting to import multiple files into microsoft access, however the With Application.FileSearch does not work and the Funtion ImportExcelFiles() returns an error. new to the script don't know how it all works.

http://www.mrexcel.com/forum/micros...olders-worth-excel-work-books-into-acess.html

Function ImportExcelFiles()
Dim Counter As Integer

With Application.FileSearch
.NewSearch
.LookIn = "\C:\ImportDir" 'change this to your actual directory
.SearchSubFolders = False 'set to True if you want to search subfolders too
FileName = "*.xls" 'changed HERE

If .Execute() > 0 Then 'files found
For Counter = 1 To .FoundFiles.Count 'loop through files
.FileName = .FoundFiles(Counter) 'set / get the file name
'Change the "ImportFile" part in the line below if you are using a different table name
'Note: 1 command for each worksheet. I have assumed they are Sheet1 etc.
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "Table1", .FileName, False, "Sheet1!"'Changed HERE
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "Table1", .FileName, False, "Sheet1!"'Changed HERE
'DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "Table1", .FileName, True, "Sheet1!"'Changed HERE

DoEvents 'don't take over all of the PC resources
Next Counter
MsgBox "Import complete.", vbInformation, "Done"
Else 'files not found
MsgBox "There were no files found.", vbCritical, "Error"
End If
End With
End Function
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try and modify for your purpose. It works on a Windows from WinXP (maybe, Win2000)
Code:
Public Sub Import()
    Dim pShell As Object, pFolder As Object
    Dim pItems As Object, pItem As Object
    Set pShell = CreateObject("Shell.Application")
    Set pFolder = pShell.Namespace("c:\ImportDir")
    Set pItems = pFolder.Items
    pItems.Filter 64, "*.xlsx"
    If pItems.Count > 0 Then
        For Each pItem In pItems
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Table1", pItem.Path, True, "Sheet1!A1:C7"
        Next
    End If
End Sub
Regards,
 
Upvote 0
I am sorry. What is mean "document names are unknown"? The code above searches files with a xlsx extension in a designated folder and puts each from them into DoCmd.TransferSpreadsheet command.
 
Last edited:
Upvote 0
I have multiple files, I would like to import them all into access and call them what they are called without the xlsx extention.
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,327
Members
451,697
Latest member
pedroDH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top