?
Solved

Import multiple Excel files into Access

Posted on 2009-07-13
4
Medium Priority
?
640 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 93

Accepted Solution

by:
Patrick Matthews earned 1000 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 93

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

752 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