View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
GregR GregR is offline
external usenet poster
 
Posts: 246
Default I want a macro that will upload a number of excel files at once.

Tim, you just about have it. The only missing part is I want to open
the most recent modified file in each subfolder. TIA

Greg
Tim Williams wrote:
Greg,
This might help.
Tim

'###########################################
Option Explicit

Sub ProcessNewestFiles()

Const S_PATH As String = "D:\Analysis"

Dim oFSO As Scripting.FileSystemObject
Dim foldSub As Folder, sFile As String

Set oFSO = New Scripting.FileSystemObject
For Each foldSub In oFSO.GetFolder(S_PATH).SubFolders
sFile = GetLastModified(foldSub.Path)
If sFile < "" Then
'*****************
'process sFile here
Debug.Print foldSub.Path & " " & sFile
'*****************
End If
Next foldSub

End Sub

'Given as folder path, return the path of the most-recently
' modified file (or "" if no files are found.
Function GetLastModified(sDirPath As String) As String

Dim oFSO As Scripting.FileSystemObject
Dim lastDate As Date
Dim f As File, retVal As String

Set oFSO = New Scripting.FileSystemObject
retVal = ""
lastDate = CDate("01/01/1901")
For Each f In oFSO.GetFolder(sDirPath).Files
If f.Name Like "*.xls" Then
If f.DateLastModified lastDate Then
lastDate = f.DateLastModified
retVal = f.Path
End If
End If
Next f

GetLastModified = retVal

End Function
'##########################################


"GregR" wrote in message ups.com...
Tim, that is correct only one level of subfolders

Greg
Tim Williams wrote:
How many levels of subfolders? Just one level under a main folder?

Tim

"GregR" wrote in message ups.com...
Otto, how do I modify to include subfolders and only run the macro on
the most recently modified file in the subfolder. TIA