View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
SeanC UK[_2_] SeanC UK[_2_] is offline
external usenet poster
 
Posts: 63
Default macro that identify the newest file in a folder and open it.

Hi Don,

One way you could do it is to look at the newest file created in the folder,
this could involve creating a File System object and examining the folder.

However, if you already know much of the filename, and it is only the date
portion that is changing, then here's an easier way to do it:

Dim strDay As String
Dim strMon As String
Dim strYear As String
Dim dteDate As Date
Dim strFilename As String
dteDate = Date
strDay = Format(dteDate, "dd")
strMon = Format(dteDate, "mmm")
strYear = Format(dteDate, "yyyy")
strFilename = "dividend.yester." & strMon & strDay & strYear & ".txt"

This will give you a filename representing today's date. Should you wish to
look at the day before then change the dteDate line to:
dteDate = DateAdd("d", -1, Date)
etc

I hope this helps,

Sean.
--
(please remember to click yes if replies you receive are helpful to you)


"Don Doan" wrote:

hi,
i have folder that contain one new file each day. It's a text format file.
The naming format is as follow:
The folder is located at C:\dividend. And if today's date is jan 1 2008 then
the file is dividend.yester.Jan012008.txt. So the next day, the new file
would be dividend.yester.Jan022008.txt. These file only created during
weekday.

I have to import the content of these text file into excel each day. Is it
possible to create a macro in excel that would automatically locate the most
recent file and import the content automatically??

Thanks.
Don