Guinaba
Board Regular
- Joined
- Sep 19, 2018
- Messages
- 233
- Office Version
- 2016
- Platform
- Windows
Hi guys,
I have the following situation:
1) Need to extract all the .xlsx files from a folder
2) Append all of them in one table
3) After having all the data in this table, need to start appending only the most recent file
I do have the vba code to get the most recent file and the other vba module to get all files, however not sure what the best approach to combine all of them in one table in access. If I use append query do I have to append one by one? Hopefully not
VBA code to extract the most recent .xlsx file
VBA code to extract all the .xlsx files
I have the following situation:
1) Need to extract all the .xlsx files from a folder
2) Append all of them in one table
3) After having all the data in this table, need to start appending only the most recent file
I do have the vba code to get the most recent file and the other vba module to get all files, however not sure what the best approach to combine all of them in one table in access. If I use append query do I have to append one by one? Hopefully not
VBA code to extract the most recent .xlsx file
VBA Code:
Option Explicit
Option Compare Database
'==========================================
' Import Data From Excel With DoCmd
'==========================================
Sub importDataFromExcelDoCmd()
' Set variables
Dim strTableName As String
Dim strFileName As String
Dim VarFileName As String
Dim blnHasHeadings As Boolean
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
'********************Getting the latest file from the folder**************************************
MyPath = "Z:\Supply Chain\Demand Planning\Coles\SY35 Total Manassen\"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xlsx", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
VarFileName = (MyPath & LatestFile) 'Latest file
'*****************Setting the data in Access*************************
' Set data
strTableName = "tbl0ColesPromoData"
strFileName = VarFileName
blnHasHeadings = True
' Import data
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, strTableName, strFileName, blnHasHeadings
End Sub
VBA code to extract all the .xlsx files
VBA Code:
[/B]
Sub ImportfromPath(path As String, intoTable As String, hasHeader As Boolean)
Dim fileName As String
'Loop through the folder & import each file
fileName = Dir(path & "\*.xlsx")
While fileName <> ""
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, intoTable, path & fileName, hasHeader
'check whether there are any more files to import
fileName = Dir()
Wend
End Sub
[B]