+ Reply to Thread
Results 1 to 10 of 10

Open File With Latest Date

  1. #1
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Open File With Latest Date

    Hi all,

    I am trying to write a macro to open a file within a folder. All the files in this folder have the same name, except the date at the end of them is different. There is no consistency to how often these reports are generated, so I need to just open the file with the latest date. I also cannot use the most recently opened / edited because we often have to look back at these files. Currently using this code and changing the date every day until I can figure this out lol:

    Please Login or Register  to view this content.
    thanks!

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Open File With Latest Date

    So, you would want to take TODAY as a starting date, construct a path and filename based on today's date, and see if that file exists? If so, open it. If not, go back one day, repeat until you find the most recent existing file?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: Open File With Latest Date

    Quote Originally Posted by JBeaucaire View Post
    So, you would want to take TODAY as a starting date, construct a path and filename based on today's date, and see if that file exists? If so, open it. If not, go back one day, repeat until you find the most recent existing file?
    Yes, that would be perfect!

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Open File With Latest Date

    Something like this:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 06-09-2015 at 04:45 PM. Reason: Fixed some typos

  5. #5
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: Open File With Latest Date

    Thanks. How is your Cnt variable working in this?

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Open File With Latest Date

    It's counting how many times we've been through the DO/LOOP before and subtracting that number from today's date to adjust the next date checked by that many days.

    First time through, CNT=0, so the date checked is TODAY-0
    Second time through CNT=1, so the date checked is TODAY-1
    and so on... until a date is checked and a file found.

    I put in an escape point, every time it unsuccessfully checks 10 dates, it stops and asks if you want to keep going.

  7. #7
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: Open File With Latest Date

    Ok that makes sense. Took me a while to understand how VBA knew Cnt = 0 to start, but I guess it's implied?

    This line:

    Please Login or Register  to view this content.
    I can tell it's being used to say if the report is found, stop looping. But not quite clear on how that works. I thought Len would result in the length of the string of the file path to sOpenLastReport. But now I'm thinking maybe zero is being used as a "False" value. If that's the case, I have no idea how length of a string would make any sense. Hope you can explain this final step!

    Thanks!

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Open File With Latest Date

    sOpenLastReport is a string variable. At the beginning of the DO we construct a string using today's date.

    Then we use the DIR(string) method to bring back the same filename from the hard drive. DIR = Directory, I guess. So it's basically saying, go get the hard drive name of this file using this string.

    If the file is not there, the DIR result is blank, NULL, so the LEN() of that result is also 0. But if the file WAS found, the result coming back will be the file's name, again, and anything that happens to be will be longer than 0. It's a poor man's "does this file exist" test.

    When it comes back when a LEN > 0, the loop is aborted and we are off to open the found file.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  9. #9
    Forum Contributor
    Join Date
    11-12-2014
    Location
    Boston, MA
    MS-Off Ver
    Office 2010
    Posts
    172

    Re: Open File With Latest Date

    Ooohhhhh I understand now. That's clever.

    I like to understand the things people help me with in this forum in case they break. I ALWAYS mark as solved and add to reputation to those who have been helpful, which you have been. So thank you, and here is to your reputation!

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Open File With Latest Date

    Thanks, see you around the forum!

+ 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] Automatically open the latest date modified excel file in a folder
    By arleutwyler in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-17-2014, 01:35 PM
  2. Replies: 9
    Last Post: 11-28-2013, 05:20 PM
  3. 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
  4. Open the latest file to copy and paste data in another file
    By mmf in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2008, 10:41 AM
  5. macro to open latest file
    By tsgol in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2005, 08:05 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