illusionek
Registered User.
- Local time
- Today, 10:40
- Joined
- Dec 31, 2013
- Messages
- 92
Hello!
I am using below Macro for importing Excel files into Access. The code works very well but unfortunately now I need to modify it. My knowledge of VBA is extremely limited (below code I just found and modified a little bit), so I would appreciate help.
I need to add two additional columns for each imported spreadsheet, which are not in the original files.
First column, named 'Customer Name', needs to retrieve information from cell A6 in Excel file, which is not part of range "name" that is imported to Access. If necessary, I could somehow include it in this range but then I still would have a problem of how to repeat a value of A6 in each row of table in Access.
Second column, named "File Name", I would like to include an Excel file name from which data was imported. Again, I would like to repeat value for each row in the table, i.e. if I have 6 rows with data from file X, I would like to repeat X.xlsx in each row in column "File Name".
I am using below Macro for importing Excel files into Access. The code works very well but unfortunately now I need to modify it. My knowledge of VBA is extremely limited (below code I just found and modified a little bit), so I would appreciate help.
I need to add two additional columns for each imported spreadsheet, which are not in the original files.
First column, named 'Customer Name', needs to retrieve information from cell A6 in Excel file, which is not part of range "name" that is imported to Access. If necessary, I could somehow include it in this range but then I still would have a problem of how to repeat a value of A6 in each row of table in Access.
Second column, named "File Name", I would like to include an Excel file name from which data was imported. Again, I would like to repeat value for each row in the table, i.e. if I have 6 rows with data from file X, I would like to repeat X.xlsx in each row in column "File Name".
Code:
Dim strFile As String
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM Data;"
' Set file directory for files to be imported
strPath = "C:\test\"
' Tell it to import all Excel files from the file directory
strFile = Dir(strPath & "*.xlsx*")
' Start loop
Do While strFile <> ""
' Import file
DoCmd.TransferSpreadsheet transfertype:=acImport, tablename:="Data", FileName:=strPath & strFile, HasFieldNames:=True, Range:="name"
' Loop to next file in directory
strFile = Dir
Loop