Importing most recent file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to import data (txt file) that changes often. Each time it changes
the name changes based on the time stamp. How can I import the most recent
filewith macro or VBA?
 
You will have to set up a filesystemobject to get the createdate from
the files in the specified directory.
 
You can try something like this:

Dim dt as Date, dtMax as Date
Dim sPath as String, sName as String
Dim sMax as String, bk as Workbook
sPath = "C:\MyFiles\" '<= change to your directory
sName = dir(sPath & "*.txt")
dtMax = 0
sMax = ""
do while sName <> ""
dt = fileDateTime(sPath & sName)
if dt > dtMax then
sMax = sName
dtMax = dt
end if
Loop
set bk = Workbooks.Open(sPath & sMax)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top