Importing from excel column headers in row 5

Drand

Registered User.
Local time
Tomorrow, 00:35
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
 
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?
 
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

Back
Top Bottom