+ Reply to Thread
Results 1 to 3 of 3

Macro to open latest file within a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    12-15-2010
    Location
    Oregon, USA
    MS-Off Ver
    Excel 2007
    Posts
    48

    Macro to open latest file within a folder

    Hi Guys,
    I have case where there are excel files arranged like this within a folder. For example:-

    XA11_df01.xlsx
    XA11_df02.xlsx
    XA11_df03.xlsx
    .
    .
    XB11_df01.xlsx
    XB11_df02.xlsx
    .
    .
    YA11_df01.xlsx
    YA11_df02.xlsx
    .
    .
    YB11_df01.xlsx
    YB11_df02.xlsx
    .
    .

    All these files are located in one folder. I want to write a macro to open the latest excel files based on lastmodified date and time for each file name like XA11, XB11, YA11, YB11. I only want to do this for excel files (".xlsx") within a folder as there are other pdf files also in the folder. I want macro to open the latest file for first file name run my code on it , then open latest file for second file name and run my code on it so on.


    Any help will be highly apprecited.

    Thanks.
    Roop
    Last edited by Roop; 01-21-2011 at 10:30 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to open latest file within a folder

    Hello Roop,

    This macro will open the most recent ".xlsx" file in the given directory based on the file name to left of the underscore. You will need to change the file path and add a call to your macro in this one. At the bottom you will see a comment (in bold text). This is where you need to add the call.
    'Written: January 22, 20111
    'Author:  Leith Ross
    
    Sub OpenMostRecent()
    
      Dim Dict As Object
      Dim DLM As Variant
      Dim FileName As String
      Dim FilePath As String
      Dim FileSpec As Variant
      Dim FileType As String
      Dim FSO As Object
      Dim Key As Variant
      Dim MostRecent As Double
      Dim R As Long
      Dim RegExp As Object
      
        FilePath = "C:\"
        FileType = "*.xlsx"
        
          Set Dict = CreateObject("Scripting.Dictionary")
          Dict.CompareMode = vbTextCompare
        
          Set FSO = CreateObject("Scripting.FileSystemObject")
        
          Set RegExp = CreateObject("VBScript.RegExp")
          RegExp.IgnoreCase = True
          RegExp.Pattern = "([0-9A-Z]+).+"
          
          FilePath = IIf(Right(FilePath, 1) <> "\", FilePath & "\", FilePath)
          FileName = Dir(FilePath & FileType)
          
            Do While FileName <> ""
              If RegExp.Test(FileName) = True Then
                Key = RegExp.Replace(FileName, "$1")
                DLM = FSO.Get(FileName).DateLastModified
                  If Not Dict.Exists(Key) Then
                    ReDim FileSpec(1)
                      FileSpec(0) = FileName
                      FileSpec(1) = DLM
                    Dict.Add Key, FileSpec
                  Else
                    FileSpec = Dict(Key)
                      If DLM > FileSpec(1) Then
                        FileSpec(0) = FileName
                        FileSpec(1) = DLM
                      End If
                    Dict(Key) = FileSpec
                  End If
              End If
              FileName = Dir()
            Loop
          
          For Each Key In Dict.Keys
            FileSpec = Dict(Key)
            Workbooks.Open FilePath & FileSpec(0)
            'Call Macro1  '<<<<< Replace Macro1 with the name of your macro
          Next Key
          
      Set Dict = Nothing
      Set FSO = Nothing
      Set RegExp = Nothing
      
    End Sub
    Last edited by Leith Ross; 01-23-2011 at 02:44 AM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-23-2012
    Location
    philadelphia, USA
    MS-Off Ver
    2007
    Posts
    1

    Re: Macro to open latest file within a folder

    Thanks Leith.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1