Access Importing excel spreadsheet with variable dates

moscott75

New Member
Joined
Jan 17, 2008
Messages
25
My code below works greate when I'm importing a file that has the same date as the date when I run the import. For example, if I run the import on 06/01/18 and the filename is TestDate_060118. xlsx, Access will import and append file to existing table. If the filename is TestDate_053118.xlsx, Access will not import the file and append to table.

All files have the prefix (TestData_), but the date changes.

How can I amend my VBA code below to import my file regardless if the date changes i.e. TestDate_060118, TestDate_053118, TestDate_053018 or any date associate with TestDate_.

_______________________________________________________________________________________
Private Sub btnimportexcel_Click()


Dim PATH As String
Dim Date1 As String

Date1 = Format(Date, "mmddyy")
PATH = "C:\Users\mosco\Desktop\Test Access\ImportNewTable\Testdata_" & Date1 & ".xlsx"

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "Master", PATH, True, "Sheet1!"


MsgBox "Worksheet imported", vbInformation + vbOKOnly, "success"


End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I tried it but it didn't work. Could you post what the complete VBA code would look like with the included function in your link. Maybe I'm not entering the advised code correctly. Appreciate your help.
 
Upvote 0
Code:
'******************************************************************************************
Sub import_file()
    
    Dim strFileName As String
    'TODO: Specify path and file spec
    Dim strFolder As String: strFolder = "C:\temp\"
    Dim strFileSpec As String: strFileSpec = strFolder & "Testdate_*.xlsx"
    
    strFileName = Dir(strFileSpec)
    Do While Len(strFileName) > 0
        'TODO: replace Debug.Print by the process you want to do on the file
        Debug.Print strFileName
        Debug.Print strFolder & strFileName
        ' rename file
        Name strFolder & strFileName As strFolder & "imported_" & strFileName
        strFileName = Dir
    Loop
    
End Sub
'******************************************************************************************

make sure your file names are correct

in the first post you say
TestDate_060118
^ DATE
but your code shows
Testdata_" & Date1 & ".xlsx"
^ DATA

 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top