+ Reply to Thread
Results 1 to 4 of 4

Open newest folder and specific file in the folder

Hybrid View

  1. #1
    Registered User
    Join Date
    01-09-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    53

    Open newest folder and specific file in the folder

    Hi guys,

    Could someone advise me on the following please.

    I would like to automatize manual process of opening a specific file in a specific folder.

    In location: "\\test\UK_test\reports\" folders get created each day, for example today: "\\test\UK_test\reports\05.05.2017\" ...this folder "05.05.2017" contains a specific excel file called"ratios_20170505" which I have to open and copy data from.

    Tomorrow new file will be created in this location, for example: "\\test\UK_test\reports\06.05.2017\" and the name of the file inside will be "ratios_20170506"

    Ideally I would like to write a macro that would always locate the latest folder and open the file which name starts with "ratios_xxxxxxxx" ?

    Many thanks for your help once again.

    Let me know if you need any other details.

    Best,
    Seb
    Last edited by timtim91; 05-09-2017 at 09:16 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Open newest folder and specific file in the folder

    Hi Tim ... the code below goes in a standard module and will search through the directory in which the file is saved (so save it somewhere before running it) and all sub-directories looking for files that start with "Ratios" and progressively identifying the latest such file. When complete it will open the file with the latest create date.
    Option Explicit
    
    Public datNewest As Date
    Public strLatestFile As String
    
    Sub OpenLatestRatioFile()
    Dim strRootDir As String
    
        datNewest = CDate("01 Jan 0001")
        strRootDir = ThisWorkbook.Path & "\"
        strLatestFile = ""
        Call GetSubDirectories(strRootDir)
        If strLatestFile <> "" Then
            Workbooks.Open Filename:=strLatestFile, UpdateLinks:=False, ignorereadonlyrecommended:=True
        Else
            MsgBox ("No file beginning with Ratios found in directory or subfolders.")
        End If
        
    End Sub
    
    Sub GetSubDirectories(strFolder As String)
    Dim objFileSystem As Object: Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    Dim objFolder As Object: Set objFolder = objFileSystem.GetFolder(strFolder)
    Dim objSubFolder As Object
        
        Call GetFiles(objFolder.Path)
        For Each objSubFolder In objFolder.subfolders
            GetSubDirectories (objFolder.Path & "\" & objSubFolder.Name)   'recursive call
        Next objSubFolder
        
    End Sub
    
    Sub GetFiles(strPath As String)
    Dim objFile As Object
    Dim objFileSystem As Object: Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    Dim objFolder As Object: Set objFolder = objFileSystem.GetFolder(strPath)
        
        On Error Resume Next
        For Each objFile In objFolder.Files
            If objFile.DateCreated > datNewest And Left(objFile.Name, 6) = "Ratios" Then
                datNewest = objFile.DateCreated
                strLatestFile = strPath & "\" & objFile.Name
            End If
        Next objFile
        On Error GoTo 0
        
    End Sub
    If you want to specify a different starting directory then change this line in the first routine:
    strRootDir = ThisWorkbook.Path & "\"
    To something like:
    strRootDir = "C:\Users\Tim\"
    Remember the path must end with an "\".
    Hope that helps. MM.
    Last edited by MatrixMan; 05-09-2017 at 11:10 AM.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  3. #3
    Registered User
    Join Date
    01-09-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    53

    Re: Open newest folder and specific file in the folder

    Hi MM,

    It works!! You saved me lots of time!

    Thanks again!!

  4. #4
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: Open newest folder and specific file in the folder

    You're very welcome ... don't forget to mark the thread closed and if would click on the star / add reputation icon, that would be appreciated too.

+ 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. [SOLVED] Copy newest file in folder to a new folder with new name
    By T_BOLT in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-01-2017, 02:25 AM
  2. How to open specific file in a folder using VBA
    By Amar321 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-28-2016, 05:30 AM
  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. importing the newest file in a folder as text
    By hotwheels in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-06-2011, 08:58 PM
  5. Open File from specific folder
    By ateam in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-26-2009, 05:28 PM
  6. Find newest file in a folder
    By duckboy1981 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-30-2008, 07:32 AM
  7. Moving all but the newest files from one folder to an Archive folder.
    By Zaphius in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-29-2007, 11:48 PM

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