Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Finding the earliest "Last Saved Date" of Excel workbooks

Can this be done. I have a series of folders for week ending reports.
Within those folders are several subfolders. What I want to know is what is
the first "last saved date" of any Excel workbooks within those folders.

Thanks in advance,
Barb Reinhardt
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Finding the earliest "Last Saved Date" of Excel workbooks

Hi Barb,

Barb Reinhardt wrote:
Can this be done. I have a series of folders for week ending
reports. Within those folders are several subfolders. What I want
to know is what is the first "last saved date" of any Excel workbooks
within those folders.


You can use the Scripting.FileSystemObject to do this type of thing. Here's
some code that you can use to find the earliest last modified Excel workbook
in a given folder. Just call it like this:

Demo "c:\"

Here's the code:

Public Sub demo(rsFolderPath As String)
Dim sPath As String
Dim dtMin As Date

If mbFindEarliestLastModifiedInFolder(rsFolderPath, _
sPath, dtMin) Then
MsgBox "The earliest last modified file in '" & rsFolderPath & _
"' is '" & sPath & "' with a date of " & Format$(dtMin, _
"mm/dd/yyyy hh:mm:ss") & ".", vbInformation, "Last Modified Date"
Else
MsgBox "No Excel workbooks found in '" & rsFolderPath & "'.", _
vbInformation, "Last Modified Date"
End If
End Sub

Private Function mbFindEarliestLastModifiedInFolder(rsFolderPath _
As String, rsFilePath As String, rdtLastModified As Date) As Boolean
Dim fso As Object
Dim fil As Object
Dim dtMin As Date
Dim dtCurr As Date
Dim sMinPath As String

On Error GoTo ErrHandler

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(rsFolderPath) Then
dtMin = Now()
For Each fil In fso.GetFolder(rsFolderPath).Files
If StrComp(fil.Type, "Microsoft Excel Worksheet", _
vbTextCompare) = 0 Then
dtCurr = fil.DateLastModified
If dtCurr < dtMin Then
dtMin = dtCurr
sMinPath = fil.Path
End If
End If
Next fil

rsFilePath = sMinPath
rdtLastModified = dtMin

mbFindEarliestLastModifiedInFolder = Len(rsFilePath)
End If

ExitRoutine:
Set fso = Nothing
Exit Function
ErrHandler:
Debug.Print Err.Number & ": " & Err.Description
Select Case Err.Number
Case 429
MsgBox "Error creating FileSystemObject.", vbExclamation, _
"Error"
Case Else
MsgBox "Unexpected error:" & vbLf & vbLf & Err.Number & _
": " & Err.Description, vbCritical, "Error"
End Select
Resume ExitRoutine
End Function

Private Function mdtGetLastModified(rsFullPath As String) As Date
Dim fso As Object
Dim fil As Object

On Error GoTo ErrHandler

Set fso = CreateObject("Scripting.FileSystemObject")

Set fil = fso.GetFile(rsFullPath)
mdtGetLastModified = fil.DateLastModified

ExitRoutine:
Set fso = Nothing
Exit Function
ErrHandler:
Debug.Print Err.Number & ": " & Err.Description
Select Case Err.Number
Case 429
MsgBox "Error creating FileSystemObject.", vbExclamation, _
"Error"
Case 53
MsgBox "Invalid file path '" & rsFullPath & "'.", _
vbExclamation, "Error"
Case Else
MsgBox "Unexpected error:" & vbLf & vbLf & Err.Number & _
": " & Err.Description, vbCritical, "Error"
End Select
Resume ExitRoutine
End Function

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Finding the earliest "Last Saved Date" of Excel workbooks

Jake, to modify it to give the latest file do I just change DtMin to
DtMax?

Greg
Jake Marx wrote:
Hi Barb,

Barb Reinhardt wrote:
Can this be done. I have a series of folders for week ending
reports. Within those folders are several subfolders. What I want
to know is what is the first "last saved date" of any Excel workbooks
within those folders.


You can use the Scripting.FileSystemObject to do this type of thing. Here's
some code that you can use to find the earliest last modified Excel workbook
in a given folder. Just call it like this:

Demo "c:\"

Here's the code:

Public Sub demo(rsFolderPath As String)
Dim sPath As String
Dim dtMin As Date

If mbFindEarliestLastModifiedInFolder(rsFolderPath, _
sPath, dtMin) Then
MsgBox "The earliest last modified file in '" & rsFolderPath & _
"' is '" & sPath & "' with a date of " & Format$(dtMin, _
"mm/dd/yyyy hh:mm:ss") & ".", vbInformation, "Last Modified Date"
Else
MsgBox "No Excel workbooks found in '" & rsFolderPath & "'.", _
vbInformation, "Last Modified Date"
End If
End Sub

Private Function mbFindEarliestLastModifiedInFolder(rsFolderPath _
As String, rsFilePath As String, rdtLastModified As Date) As Boolean
Dim fso As Object
Dim fil As Object
Dim dtMin As Date
Dim dtCurr As Date
Dim sMinPath As String

On Error GoTo ErrHandler

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(rsFolderPath) Then
dtMin = Now()
For Each fil In fso.GetFolder(rsFolderPath).Files
If StrComp(fil.Type, "Microsoft Excel Worksheet", _
vbTextCompare) = 0 Then
dtCurr = fil.DateLastModified
If dtCurr < dtMin Then
dtMin = dtCurr
sMinPath = fil.Path
End If
End If
Next fil

rsFilePath = sMinPath
rdtLastModified = dtMin

mbFindEarliestLastModifiedInFolder = Len(rsFilePath)
End If

ExitRoutine:
Set fso = Nothing
Exit Function
ErrHandler:
Debug.Print Err.Number & ": " & Err.Description
Select Case Err.Number
Case 429
MsgBox "Error creating FileSystemObject.", vbExclamation, _
"Error"
Case Else
MsgBox "Unexpected error:" & vbLf & vbLf & Err.Number & _
": " & Err.Description, vbCritical, "Error"
End Select
Resume ExitRoutine
End Function

Private Function mdtGetLastModified(rsFullPath As String) As Date
Dim fso As Object
Dim fil As Object

On Error GoTo ErrHandler

Set fso = CreateObject("Scripting.FileSystemObject")

Set fil = fso.GetFile(rsFullPath)
mdtGetLastModified = fil.DateLastModified

ExitRoutine:
Set fso = Nothing
Exit Function
ErrHandler:
Debug.Print Err.Number & ": " & Err.Description
Select Case Err.Number
Case 429
MsgBox "Error creating FileSystemObject.", vbExclamation, _
"Error"
Case 53
MsgBox "Invalid file path '" & rsFullPath & "'.", _
vbExclamation, "Error"
Case Else
MsgBox "Unexpected error:" & vbLf & vbLf & Err.Number & _
": " & Err.Description, vbCritical, "Error"
End Select
Resume ExitRoutine
End Function

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Finding the earliest "Last Saved Date" of Excel workbooks

Hi Greg,

GregR wrote:
Jake, to modify it to give the latest file do I just change DtMin to
DtMax?


You'd have to change a few things (most are cosmetic), but not much:

1) do a find/replace on dtMin -- dtMax

2) do a find/replace on mbFindEarliestLastModifiedInFolder --
mbFindLastModifiedInFolder

3) change this line:

MsgBox "The earliest last modified file in '" & rsFolderPath
to
MsgBox "The last modified file in '" & rsFolderPath

4) change this line:

dtMax = Now()
to
dtMax = 0

5) change this line:

If dtCurr < dtMax Then
to
If dtCurr dtMax Then


I think that's it. The most important changes are in 4 & 5 - the others are
cosmetic only.

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]

Jake Marx wrote:
Hi Barb,

Barb Reinhardt wrote:
Can this be done. I have a series of folders for week ending
reports. Within those folders are several subfolders. What I want
to know is what is the first "last saved date" of any Excel
workbooks within those folders.


You can use the Scripting.FileSystemObject to do this type of thing.
Here's some code that you can use to find the earliest last modified
Excel workbook in a given folder. Just call it like this:

Demo "c:\"

Here's the code:

Public Sub demo(rsFolderPath As String)
Dim sPath As String
Dim dtMin As Date

If mbFindEarliestLastModifiedInFolder(rsFolderPath, _
sPath, dtMin) Then
MsgBox "The earliest last modified file in '" & rsFolderPath
& _ "' is '" & sPath & "' with a date of " & Format$(dtMin,
_ "mm/dd/yyyy hh:mm:ss") & ".", vbInformation, "Last
Modified Date" Else
MsgBox "No Excel workbooks found in '" & rsFolderPath &
"'.", _ vbInformation, "Last Modified Date"
End If
End Sub

Private Function mbFindEarliestLastModifiedInFolder(rsFolderPath _
As String, rsFilePath As String, rdtLastModified As Date) As Boolean
Dim fso As Object
Dim fil As Object
Dim dtMin As Date
Dim dtCurr As Date
Dim sMinPath As String

On Error GoTo ErrHandler

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(rsFolderPath) Then
dtMin = Now()
For Each fil In fso.GetFolder(rsFolderPath).Files
If StrComp(fil.Type, "Microsoft Excel Worksheet", _
vbTextCompare) = 0 Then
dtCurr = fil.DateLastModified
If dtCurr < dtMin Then
dtMin = dtCurr
sMinPath = fil.Path
End If
End If
Next fil

rsFilePath = sMinPath
rdtLastModified = dtMin

mbFindEarliestLastModifiedInFolder = Len(rsFilePath)
End If

ExitRoutine:
Set fso = Nothing
Exit Function
ErrHandler:
Debug.Print Err.Number & ": " & Err.Description
Select Case Err.Number
Case 429
MsgBox "Error creating FileSystemObject.",
vbExclamation, _ "Error"
Case Else
MsgBox "Unexpected error:" & vbLf & vbLf & Err.Number & _
": " & Err.Description, vbCritical, "Error"
End Select
Resume ExitRoutine
End Function

Private Function mdtGetLastModified(rsFullPath As String) As Date
Dim fso As Object
Dim fil As Object

On Error GoTo ErrHandler

Set fso = CreateObject("Scripting.FileSystemObject")

Set fil = fso.GetFile(rsFullPath)
mdtGetLastModified = fil.DateLastModified

ExitRoutine:
Set fso = Nothing
Exit Function
ErrHandler:
Debug.Print Err.Number & ": " & Err.Description
Select Case Err.Number
Case 429
MsgBox "Error creating FileSystemObject.",
vbExclamation, _ "Error"
Case 53
MsgBox "Invalid file path '" & rsFullPath & "'.", _
vbExclamation, "Error"
Case Else
MsgBox "Unexpected error:" & vbLf & vbLf & Err.Number & _
": " & Err.Description, vbCritical, "Error"
End Select
Resume ExitRoutine
End Function

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default Finding the earliest "Last Saved Date" of Excel workbooks

Jake, thanks worked like a charm. If I may impinge on you for one more
question, If I want to include subfoders ( one level) and report the
last modified in each subfolder is there much of a modification. TIA

Greg
Jake Marx wrote:
Hi Greg,

GregR wrote:
Jake, to modify it to give the latest file do I just change DtMin to
DtMax?


You'd have to change a few things (most are cosmetic), but not much:

1) do a find/replace on dtMin -- dtMax

2) do a find/replace on mbFindEarliestLastModifiedInFolder --
mbFindLastModifiedInFolder

3) change this line:

MsgBox "The earliest last modified file in '" & rsFolderPath
to
MsgBox "The last modified file in '" & rsFolderPath

4) change this line:

dtMax = Now()
to
dtMax = 0

5) change this line:

If dtCurr < dtMax Then
to
If dtCurr dtMax Then


I think that's it. The most important changes are in 4 & 5 - the others are
cosmetic only.

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]

Jake Marx wrote:
Hi Barb,

Barb Reinhardt wrote:
Can this be done. I have a series of folders for week ending
reports. Within those folders are several subfolders. What I want
to know is what is the first "last saved date" of any Excel
workbooks within those folders.

You can use the Scripting.FileSystemObject to do this type of thing.
Here's some code that you can use to find the earliest last modified
Excel workbook in a given folder. Just call it like this:

Demo "c:\"

Here's the code:

Public Sub demo(rsFolderPath As String)
Dim sPath As String
Dim dtMin As Date

If mbFindEarliestLastModifiedInFolder(rsFolderPath, _
sPath, dtMin) Then
MsgBox "The earliest last modified file in '" & rsFolderPath
& _ "' is '" & sPath & "' with a date of " & Format$(dtMin,
_ "mm/dd/yyyy hh:mm:ss") & ".", vbInformation, "Last
Modified Date" Else
MsgBox "No Excel workbooks found in '" & rsFolderPath &
"'.", _ vbInformation, "Last Modified Date"
End If
End Sub

Private Function mbFindEarliestLastModifiedInFolder(rsFolderPath _
As String, rsFilePath As String, rdtLastModified As Date) As Boolean
Dim fso As Object
Dim fil As Object
Dim dtMin As Date
Dim dtCurr As Date
Dim sMinPath As String

On Error GoTo ErrHandler

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(rsFolderPath) Then
dtMin = Now()
For Each fil In fso.GetFolder(rsFolderPath).Files
If StrComp(fil.Type, "Microsoft Excel Worksheet", _
vbTextCompare) = 0 Then
dtCurr = fil.DateLastModified
If dtCurr < dtMin Then
dtMin = dtCurr
sMinPath = fil.Path
End If
End If
Next fil

rsFilePath = sMinPath
rdtLastModified = dtMin

mbFindEarliestLastModifiedInFolder = Len(rsFilePath)
End If

ExitRoutine:
Set fso = Nothing
Exit Function
ErrHandler:
Debug.Print Err.Number & ": " & Err.Description
Select Case Err.Number
Case 429
MsgBox "Error creating FileSystemObject.",
vbExclamation, _ "Error"
Case Else
MsgBox "Unexpected error:" & vbLf & vbLf & Err.Number & _
": " & Err.Description, vbCritical, "Error"
End Select
Resume ExitRoutine
End Function

Private Function mdtGetLastModified(rsFullPath As String) As Date
Dim fso As Object
Dim fil As Object

On Error GoTo ErrHandler

Set fso = CreateObject("Scripting.FileSystemObject")

Set fil = fso.GetFile(rsFullPath)
mdtGetLastModified = fil.DateLastModified

ExitRoutine:
Set fso = Nothing
Exit Function
ErrHandler:
Debug.Print Err.Number & ": " & Err.Description
Select Case Err.Number
Case 429
MsgBox "Error creating FileSystemObject.",
vbExclamation, _ "Error"
Case 53
MsgBox "Invalid file path '" & rsFullPath & "'.", _
vbExclamation, "Error"
Case Else
MsgBox "Unexpected error:" & vbLf & vbLf & Err.Number & _
": " & Err.Description, vbCritical, "Error"
End Select
Resume ExitRoutine
End Function

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Finding the earliest "Last Saved Date" of Excel workbooks

Hi Greg,

GregR wrote:
Jake, thanks worked like a charm. If I may impinge on you for one more
question, If I want to include subfoders ( one level) and report the
last modified in each subfolder is there much of a modification. TIA


No problem. To do this, you could use recursion on the Demo subroutine.
However, this will traverse all subfolders of the folder you pass in (not
just one level):

Public Sub demo(rsFolderPath As String)
Dim sPath As String
Dim dtMin As Date
Dim fso As Object
Dim fol As Object

If mbFindEarliestLastModifiedInFolder(rsFolderPath, _
sPath, dtMin) Then
MsgBox "The earliest last modified file in '" & rsFolderPath & _
"' is '" & sPath & "' with a date of " & Format$(dtMin, _
"mm/dd/yyyy hh:mm:ss") & ".", vbInformation, "Last Modified Date"
Else
MsgBox "No Excel workbooks found in '" & rsFolderPath & "'.", _
vbInformation, "Last Modified Date"
End If

Set fso = CreateObject("Scripting.FileSystemObject")
For Each fol In fso.getfolder(rsFolderPath).Subfolders
demo fol.Path
Next fol

Set fso = Nothing
End Sub

If you want just one level, you could try this:

Public Sub demo(rsFolderPath As String, rsOrigFolderPath As String)
Dim sPath As String
Dim dtMin As Date
Dim fso As Object
Dim fol As Object

If mbFindEarliestLastModifiedInFolder(rsFolderPath, _
sPath, dtMin) Then
MsgBox "The earliest last modified file in '" & rsFolderPath & _
"' is '" & sPath & "' with a date of " & Format$(dtMin, _
"mm/dd/yyyy hh:mm:ss") & ".", vbInformation, "Last Modified Date"
Else
MsgBox "No Excel workbooks found in '" & rsFolderPath & "'.", _
vbInformation, "Last Modified Date"
End If



If rsFolderPath = rsOrigFolderPath Then
Set fso = CreateObject("Scripting.FileSystemObject")
For Each fol In fso.getfolder(rsFolderPath).Subfolders
demo fol.Path, rsOrigFolderPath
Next fol
Set fso = Nothing
End If
End Sub


--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]


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
Is there a "last saved on date/user" macro/function for Excel 2003 Zliz Excel Discussion (Misc queries) 2 January 2nd 07 10:12 PM
"Last Saved" date in Excel 2000 Rachael Excel Worksheet Functions 11 September 21st 06 05:50 PM
how do I insert "last saved date" in as a field in excel? chirag Excel Worksheet Functions 5 February 15th 06 05:42 PM
How do I put a "date & time saved" stamp inside an Excel Wksht? Bill Excel Programming 1 May 12th 05 05:03 PM
How do I put a "date & time saved" stamp inside an Excel Wksht? Mangesh Yadav[_2_] Excel Programming 0 May 10th 05 12:20 PM


All times are GMT +1. The time now is 06:07 PM.

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"