Importing from excel column headers in row 5 (1 Viewer)

Drand

Registered User.
Local time
Today, 16:46
Joined
Jun 8, 2019
Messages
179
Hi all

My client has modified their excel spreadsheets so the headers are now in row 5 instead of row 1. How can I modify the following function to reflect the changes?

Code:
Public Function ImportHierarchyFunctionFiles()

    Dim MyFile As String
Dim MyPath As String
Dim db As Database

Set db = CurrentDb ' Initialize db object
        db.Execute "DELETE FROM tblFunction"
        db.Execute "DELETE FROM tblLocation"

MyPath = "C:\KPMG\Hierarchy data\"
MyFile = Dir(MyPath & "*.xlsx")
Do While MyFile > vbNullString
    
    
    DoCmd.TransferSpreadsheet acImport, 10, "tblFunction", MyPath & MyFile, True, "Function Hierarchy!"
    DoCmd.TransferSpreadsheet acImport, 10, "tblLocation", MyPath & MyFile, True, "Location Hierarchy!"
    MyFile = Dir
   ' MsgBox "Raw Data Imported for " & MyFile & " From " & MyPath
    
    
Loop


End Function

Many thanks
 

Drand

Registered User.
Local time
Today, 16:46
Joined
Jun 8, 2019
Messages
179
Thanks for your response. The function loops through 134 spreadsheets and each has a different range of data, albeit each starting in cell A5.

How would I select the individual ranges in each spreadsheet?
 

June7

AWF VIP
Local time
Yesterday, 22:46
Joined
Mar 9, 2014
Messages
5,471
Options:

1. design workbook with name assigned to range for import - it can be the same name in each

2. VBA Excel automation to determine last populated row and column on sheet or delete first 4 rows

For more info, review http://accessmvp.com/KDSnell/EXCEL_Import.htm
 

ebs17

Well-known member
Local time
Today, 08:46
Joined
Feb 7, 2020
Messages
1,946
a different range of data
If the number of rows is different, you can import using an append query, making the range large enough and filtering out empty rows.
Code:
    sSQL = "INSERT INTO AccessTable( Key1, Key2, Value1, Value2, Timestampfield )" & _
           " SELECT DISTINCT E.Key1, E.Key2, E.Value1, E.Value2, Now()" & _
           " FROM [excel 12.0 xml;hdr=yes;imex=1;DATABASE=" & ExcelFile & "].[" & SheetName & "$A5:D1000] AS E" & _
           " LEFT JOIN AccessTable AS A ON E.Key1 = A.Key1 AND E.Key2 = A.Key2 WHERE A.Key1 Is Null AND E.Key1 > 0"
Four columns were used here, with two columns in combination being the key for duplicates (which are to be excluded).
 
Last edited:

Users who are viewing this thread

Top Bottom