Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Les Les is offline
external usenet poster
 
Posts: 240
Default Find latest file in folder & Open

Hi all, i would like to find the latest file in a folder and open it with
code. The files would have different names as they are updates with a code on
the end. Could somebody help me with this please.

--
Les
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default Find latest file in folder & Open

Try this:

Sub AAAAA()
Const FilePath = "D:\Data\"
Workbooks.Open Filename:=FindNewestFile(FilePath)
End Sub

Function FindNewestFile(FilePath As String) As String
Dim LastDate As Date, NewDate As Date
Dim LastFile As String, NewFile As String
'Check all the .XLS files in the folder. Find the
'most recent file.
LastFile$ = LCase$(Dir(FilePath$ & "*.XLS"))
LastDate = FileDateTime(FilePath$ & LastFile$)
NewFile$ = LastFile$
Do While Len(NewFile$) 0
NewFile$ = LCase$(Dir())
If Len(NewFile$) = 0 Then Exit Do
NewDate = FileDateTime(FilePath$ & NewFile$)
If NewDate LastDate Then
LastDate = NewDate
LastFile$ = NewFile$
End If
Loop
FindNewestFile$ = FilePath$ & LastFile$
End Function

Hope this helps,

Hutch

"Les" wrote:

Hi all, i would like to find the latest file in a folder and open it with
code. The files would have different names as they are updates with a code on
the end. Could somebody help me with this please.

--
Les

  #3   Report Post  
Posted to microsoft.public.excel.programming
Les Les is offline
external usenet poster
 
Posts: 240
Default Find latest file in folder & Open

Thanks Tom
--
Les


"Tom Hutchins" wrote:

Try this:

Sub AAAAA()
Const FilePath = "D:\Data\"
Workbooks.Open Filename:=FindNewestFile(FilePath)
End Sub

Function FindNewestFile(FilePath As String) As String
Dim LastDate As Date, NewDate As Date
Dim LastFile As String, NewFile As String
'Check all the .XLS files in the folder. Find the
'most recent file.
LastFile$ = LCase$(Dir(FilePath$ & "*.XLS"))
LastDate = FileDateTime(FilePath$ & LastFile$)
NewFile$ = LastFile$
Do While Len(NewFile$) 0
NewFile$ = LCase$(Dir())
If Len(NewFile$) = 0 Then Exit Do
NewDate = FileDateTime(FilePath$ & NewFile$)
If NewDate LastDate Then
LastDate = NewDate
LastFile$ = NewFile$
End If
Loop
FindNewestFile$ = FilePath$ & LastFile$
End Function

Hope this helps,

Hutch

"Les" wrote:

Hi all, i would like to find the latest file in a folder and open it with
code. The files would have different names as they are updates with a code on
the end. Could somebody help me with this please.

--
Les

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
find latest date modified file Junior728 Excel Programming 1 June 6th 07 10:56 AM
VBA can't find file as it moves from folder to folder pdberger Excel Programming 1 March 8th 07 01:08 AM
macro to open latest file tsgol Excel Programming 1 August 26th 05 12:37 AM
open file from folder save in new folder tim64[_3_] Excel Programming 20 June 17th 05 07:58 PM
Find folder with next to latest date Ed[_18_] Excel Programming 2 July 28th 04 08:51 PM


All times are GMT +1. The time now is 05:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"