Solved

Import multiple Excel files into Access

Posted on 2009-07-13
4
629 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
  • 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now