Working with Access 2013
I have searched far and low and cannot find syntax that I can get to work, I have attempted to piece together code to accomplish the task of opening up each excel file within a folder and replace the data in a table with data from that excel file, run a query and then repeat for each file.
I get the error code 1004 after it opens Excel, but not my desired workbook. I have to be close because the error message correctly calls out the file I am trying to open.
"Sorry, we couldn't find Nike.xlsx. Is it possible it was moved, renamed or deleted?"
Errors out on the code in red
Code:
Sub ImportfromPath()
Dim directory As String, filename As String
Dim Mywb As Workbook
Dim app As New Excel.Application
Set app = CreateObject("Excel.Application")
app.Visible = True
directory = "C:\Users\mcz0pjy\Desktop\Tracker\Templates\"
filename = Dir(directory & "*.xl??")
Do While filename <> ""
filename = filename
Set wb = app.Workbooks.Open(filename)
Debug.Print app.Version
Set xlwrksht = wb.Worksheets("Accounts")
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'clearing/reseting tables
DoCmd.OpenTable "Accounts", acViewNormal, acEdit
DoCmd.runSQL "Delete*From [Accounts]"
DoCmd.OpenTable "Results", acViewNormal, acEdit
DoCmd.runSQL "Delete*From [Results]"
'Count Rows of data in Excel file
lastrow = xlwrksht.Cells(xlwrksht.Rows.Count, 1).End(xlUp).Row
'Import data from Excel
strExcelPath = filename
Call DoCmd.TransferSpreadsheet(acImport, _
acSpreadsheetTypeExcel12, "Accounts", strExcelPath, _
True, "Accounts!A:A" & lastrow)
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Refresh Data
DoCmd.SelectObject acTable, "Accounts"
DoCmd.Requery
DoCmd.GoToRecord acDataTable, "Accounts", acLast
'Run the Dir QRY to populate info for selected accounts
Call RunDIR
filename = Dir()
Loop
app.Quit
End Sub