Solved Importing Multiple Excel Files (1 Viewer)

Drand

Registered User.
Local time
Today, 09:19
Joined
Jun 8, 2019
Messages
179
Hi

I need to import over 20 excel files into one table in a new project.

I have found the following code to do this:



Code:
Public Function Impo_allExcel()

Dim myfile
Dim mypath


mypath = "C:\KPMG\Country_Data"
ChDir (mypath)

myfile = Dir()
Do While myfile <> ""
  If myfile Like "*.xlsx" Then
     'this will import ALL the excel files
     '(one at a time, but automatically) in this folder.
     ' Make sure that's what you want.
    DoCmd.TransferSpreadsheet acImport, 8, "tblConsolRawData", mypath & myfile
  End If
  myfile = Dir()
Wend

End Function

The code is producing a "wend without while error".

Could someone please have a look at this and let me know what is wrong.

Many thanks
 

bob fitz

AWF VIP
Local time
Today, 00:19
Joined
May 23, 2011
Messages
4,727
Replace "Wend" with "Loop" and it will compile.
 

Drand

Registered User.
Local time
Today, 09:19
Joined
Jun 8, 2019
Messages
179
Thanks for that.

Now I am getting an "invalid procedure call or argument error" at
Code:
myfile = Dir()

Thanks
 

ebs17

Well-known member
Local time
Today, 01:19
Joined
Feb 7, 2020
Messages
1,949
Code:
Dim myfile AS String
Dim mypath As String

mypath = "C:\KPMG\Country_Data\"
myfile = Dir(mypath & "*.xlsx")
Do While myfile > vbNullstring
    ' Debug.Print myfile
    DoCmd.TransferSpreadsheet acImport, 10, "tblConsolRawData", mypath & myfile      ' acSpreadsheetTypeExcel12Xml
    myfile = Dir
Loop
SpreadsheetType = 8 refers to Excel 97. This does not match XLSX.
 

Drand

Registered User.
Local time
Today, 09:19
Joined
Jun 8, 2019
Messages
179
Thanks for this. Much appreciated.

When I run the code, I encounter the error "Field 'F 1' does not exist in destination table "tblConsolRawData".

There are 19 fields in each of the spreadsheets and my field names in my table correspond to these.

I have tried a couple of things to test this.

If I rename the fields in my table to F1, F2............F19 then the import works fine but this is not really practical.

If I import one file into a new table using the wizard and click "First Row Contains Column Headings", this performs perfectly.

Would appreciate any advice on this.

Thank you
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:19
Joined
Sep 21, 2011
Messages
14,317
Do all the other sheets contain headings?
Walk through your code, line by line with a breakpoint on the docmd line
 

Drand

Registered User.
Local time
Today, 09:19
Joined
Jun 8, 2019
Messages
179
Yes, all sheets contain the same headings. The code runs to the breakpoint.
 

Drand

Registered User.
Local time
Today, 09:19
Joined
Jun 8, 2019
Messages
179
The first file in the directory, Argentina Data.xlsx
 

CJ_London

Super Moderator
Staff member
Local time
Today, 00:19
Joined
Feb 19, 2013
Messages
16,618
Not at my computer to check but you are not specifying headers in transfer spreadsheet - think it defaults to false
 

ebs17

Well-known member
Local time
Today, 01:19
Joined
Feb 7, 2020
Messages
1,949
DoCmd.TransferSpreadsheet(TransferType, SpreadsheetType, TableName, FileName, HasFieldNames, Range, UseOA)

Use the HasFieldNames argument => True
F1, F2, ... are substitute names for the columns if you don't use the existing field names.
 

Drand

Registered User.
Local time
Today, 09:19
Joined
Jun 8, 2019
Messages
179
Thank you all. When I included the has field names as true it worked perfectly.

Really appreciate the assistance from everyone!
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:19
Joined
Sep 21, 2011
Messages
14,317
Look up the actual syntax next time. :(
You cannot just make stuff up with computers. They are very picky. :)
 

Drand

Registered User.
Local time
Today, 09:19
Joined
Jun 8, 2019
Messages
179
Sorry. Still have the learner plates on!
 

Users who are viewing this thread

Top Bottom