+ Reply to Thread
Results 1 to 4 of 4

Automatically open the latest date modified excel file in a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    02-12-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    58

    Automatically open the latest date modified excel file in a folder

    Hi excel Peeps! I am trying to use VBA to automatically open an existing excel file in a folder in my documents with the lastest date modified. I seem to be running into a problem with the code below. When the file path is
    sPath = ThisWorkbook.Path
    it finds the newest file and opens it but I need to specify the file path and when I set the path to
    sPath = "C:\Users\LEU70963\Documents\TRACKER JUNK\AT"
    I get a "Run-time error '1004': "could not be found. Check the spelling of the file name, and verify that the file location is correct."

    Any help would be huge!

    Sub OpenLastModifiedFile()
     Dim objFSO As Object, objFolder As Object, objFile As Object
     Dim tmpDate As Date, sFile As String, sPath As String
     
     'sPath = ThisWorkbook.Path
    
     sPath = "C:\Users\LEU70963\Documents\TRACKER JUNK\AT"
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFolder = objFSO.GetFolder(sPath)
     For Each objFile In objFolder.Files
        If objFSO.GetExtensionName(objFile.Path) = "xls" Then
            If objFile.DateLastModified > tmpDate Then
                tmpDate = objFile.DateLastModified
                sFile = objFile.Path
            End If
        End If
     Next
     Set objFSO = Nothing
     Set objFolder = Nothing
     Set objFile = Nothing
      
     Workbooks.Open sFilef
     
    End Sub
    Thank you
    Alex

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Automatically open the latest date modified excel file in a folder

    Typo?

    Workbooks.Open sFilef
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    02-12-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Automatically open the latest date modified excel file in a folder

    Hey Alphafrog, sorry about that, that must just have been an erroneous letter I added while pasting the code. I dont have that in my workbook. I believe the real problem I am having has to do with the code trying to look in a specific file directory, as I dont have any issues when it searches the current workbook path. I have included a workbook with an example of what I am talking about. If you open the "macro latest opener" and click the blue button it should try to open the "New" excel spreadsheet in the attached folder, but if you change the path/directory in the code to a specific path I get the error described above. Let me know if this clear.

    Thanks again!


    Sub OpenLastModifiedFile()
     Dim objFSO As Object, objFolder As Object, objFile As Object
     Dim tmpDate As Date, sFile As String, sPath As String
     
     'sPath = ThisWorkbook.Path
    
     sPath = "C:\Users\LEU70963\Documents\TRACKER JUNK\AT"
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFolder = objFSO.GetFolder(sPath)
     For Each objFile In objFolder.Files
        If objFSO.GetExtensionName(objFile.Path) = "xls" Then
            If objFile.DateLastModified > tmpDate Then
                tmpDate = objFile.DateLastModified
                sFile = objFile.Path
            End If
        End If
     Next
     Set objFSO = Nothing
     Set objFolder = Nothing
     Set objFile = Nothing
      
    Workbooks.Open sFile
     
    End Sub
    Open Most Recent xls.zip

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Automatically open the latest date modified excel file in a folder

    Your code works for me using a specific defined path string.

    My guess is there is something wrong with your specific defined path which I cannot test. Is your path complete? Are the files you're looking at within an additional sub-directory?
    e.g;
    sPath = "C:\Users\LEU70963\Documents\TRACKER JUNK\ATT\Open Most Recent xls"

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 9
    Last Post: 11-28-2013, 05:20 PM
  2. Open the 2nd latest file in a folder?
    By guydixon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-27-2013, 01:37 PM
  3. open the newest excel file in a folder based on modified date
    By dulitul in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-17-2013, 12:52 PM
  4. Macro to open latest file within a folder
    By Roop in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2012, 02:56 PM
  5. Excel File Open Box - want Date Modified
    By Titian in forum Excel General
    Replies: 2
    Last Post: 02-26-2006, 04:30 AM

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