Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 396
Default Help to change code

Hi all, Tom Hutchins helped me out with some code and i need to change
it.
The code loops through a folder and looks at the last time the file was
modified to open the latest file and i need to look at the date stamp
when the file was created to get the newest file and not the last
modified.
Any help would be very much appreciated.

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


Best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Help to change code

Just switch the test?

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

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Les Stout" wrote in message
...
Hi all, Tom Hutchins helped me out with some code and i need to change
it.
The code loops through a folder and looks at the last time the file was
modified to open the latest file and i need to look at the date stamp
when the file was created to get the newest file and not the last
modified.
Any help would be very much appreciated.

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


Best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 396
Default Help to change code

Hello Bob, Perhaps i did not explain myself properly, i need to read the
"Created" date stamp and not the "modified" date stamp. Will switching
the test do that ?

I have totally different code to get the "Created" Date and i am not
sure how to intergrate it.

Your help is appreciated.

Best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Help to change code

Hi Les

This modification should do the trick.

Function FindNewestFile(FilePath As String) As String
Dim LastDate As Date, NewDate As Date
Dim LastFile As String, NewFile As String
Dim fs, f, s
Set fs = CreateObject("Scripting.FileSystemObject")
'Check all the .XLS files in the folder. Find the
'file last created.
LastFile$ = LCase$(Dir(FilePath$ & "*.XLS"))

Set f = fs.getfile(FilePath$ & LastFile$)
LastDate = f.DateCreated
Set f = Nothing
NewFile$ = LastFile$
Do While Len(NewFile$) 0
NewFile$ = LCase$(Dir())
If Len(NewFile$) = 0 Then Exit Do
Set f = fs.getfile(FilePath$ & NewFile$)
NewDate = f.DateCreated
If NewDate LastDate Then
LastDate = NewDate
LastFile$ = NewFile$
End If
Loop
FindNewestFile$ = FilePath$ & LastFile$
End Function

Best regards,

Per

"Les Stout" skrev i meddelelsen
...
Hi all, Tom Hutchins helped me out with some code and i need to change
it.
The code loops through a folder and looks at the last time the file was
modified to open the latest file and i need to look at the date stamp
when the file was created to get the newest file and not the last
modified.
Any help would be very much appreciated.

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


Best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 396
Default Help to change code

Thanks Par, much appreciated... :-)

Best regards,

Les Stout

*** Sent via Developersdex http://www.developersdex.com ***
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
Code to change code in a sheet and workbook module Otto Moehrbach Excel Programming 11 November 11th 07 07:20 PM
Change code with code? CLR Excel Programming 2 April 25th 07 07:40 PM
Change code by code. Can you do that? Corey Excel Programming 2 March 1st 07 12:52 AM
Can I use code/macro to change code/macro in an existing file? Scott Bedows Excel Programming 2 February 14th 07 05:50 AM
Code Conflicts With Worksheet Change Code Paige Excel Programming 3 March 3rd 06 04:25 PM


All times are GMT +1. The time now is 08:39 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"