Solved

Import multiple Excel files into Access

Posted on 2009-07-13
4
637 Views
Last Modified: 2013-11-27
I have a bunch of individual Excel files. They are all contained in the same folder. Each one only has a few records. They all have the same format (data in the first 5 columns only). I'm looking for an import program that will import all the files in the folder so that I don't have to manually do a "get external data" for each individual file since there are like a 100 of them and I will be having to do this task frequently in the future.  Or what also would be fine is a macro to combine all of the excel files into one big file. Either way is fine.... a program in Access or a program in Excel. Thanks.
0
Comment
Question by:dbfromnewjersey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 24842897
Hello dbfromnewjersey,

Assuming you want all Excel files in a given folder...

Dim fso As Object, fld As Object, fil As Object

Set fso = CreateObject("Scripting.FIleSystemObject
Set fld = fso.GetFolder("c:\folder\subfolder")

For Each fil In fld.Files
    If LCase(Right(fil.Name, 4)) = ".xls" Then
        DoCmd.TransferSpreadsheet TransferType:=acImport, _
            TableName:= "NameOfTable", FileName:=fil.Path, _
            HasFieldNames:=True
    End If
Next

Set fil = Nothing
Set fld = Nothing
Set fso = Nothing

Regards,

Patrick
0
 

Author Comment

by:dbfromnewjersey
ID: 24845272
I put that code under a command button on a form, changed the to the right of GetFolder and changed TableName to my table name. When I run the code, nothing happens. Any thoughts?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24845304
dbfromnewjersey,

Please paste back the code exactly as you implemented it.

Regards,

Patrick
0
 

Author Comment

by:dbfromnewjersey
ID: 24845414
Now I'm getting an error saying: "Run-time error 2391".  Field 'F1' doesn't exist in destination table tblTEST.  I should mention that my excel files do not have field names/headers. The data starts on row 1 of each file. There is nothing in the folder other than the files I need to import. I also tried commenting out the "If Then" logic since the only things in the folder are excel files. Here is the code under the command button:

Private Sub Command0_Click()
Dim fso As Object, fld As Object, fil As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("F:\TESTFOLDER")
For Each fil In fld.Files
    If LCase(Right(fil.Name, 4)) = ".xls" Then
        DoCmd.TransferSpreadsheet TransferType:=acImport, _
            TableName:="tblTEST", FileName:=fil.Path, _
            HasFieldNames:=False
    End If
Next

Set fil = Nothing
Set fld = Nothing
Set fso = Nothing

End Sub
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 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