+ Reply to Thread
Results 1 to 3 of 3

VBA loop to open most recent day's excel workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    02-10-2013
    Location
    Really
    MS-Off Ver
    Excel 2007
    Posts
    25

    VBA loop to open most recent day's excel workbook

    So I have a VBA code to open a workbook saved with yesterday's date. It looks like the following:

    Sub OpenYesterday
    Dim OpenPath As String

    OpenPath = "N:\File\": & "today_" (Now(),-1, "YYYYMMDD") & ".xlsx"

    Workbooks.Open Filename:=OpenPath
    EndSub

    Obviously I have my full filepath in the code but how can I loop this so it will open the last workbook that has been saved? For example, on Monday I would want it to pull Friday's report. I save a workbook every weekday with that days date. Example: Today is 1/13/2014 and I want this code to open the workbook that was saved on Friday 1/10/2014. I have tried the "WorkDay" function which works as well but lets say I didn't save a workbook today (1/13/2014) so tomorrow the last workbook would still be Friday's 1/10/2014 workbook. Is there a way to loop this code so that it will continually look for yesterday's workbook and if there is not a workbook from yesterday it would loop back two days until the 1/11/2014 workbook, then if that isn't saved it would loop back to the 1/10/2014 workbook? So tomorrow if there is no workbook saved with today's 1/13/2014 workbook it would loop 3 times to find the workbook saved on 1/10/2014?

    Sorry if this is confusing, let me know if you need further explanation.

    Thank you.

    Im thinking I will just need it to loop back to "Now(), -2" if there is no workbook from yesterday, and "Now(), -3" if there is no report from 2 days ago, etc...
    Last edited by richter394; 01-13-2014 at 09:34 PM.

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: VBA loop to open most recent day's excel workbook

    If the last created filename in a folder is needed, the code is even easier.

    Sub ken()
      Dim i As Integer, OpenPath As String
      For i = 1 To 31
        OpenPath = "N:\File\" & "today_" & Format(Date - i, "YYYYMMDD") & ".xlsx"
        If Dir(OpenPath) <> "" Then Exit For
      Next i
      
      If Dir(OpenPath) = "" Then
        MsgBox "Oops, macro ending as no file can be found within 31 days."
        Exit Sub
      End If
      
      ' your code
    End Sub

  3. #3
    Registered User
    Join Date
    02-10-2013
    Location
    Really
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: VBA loop to open most recent day's excel workbook

    Okay, that is helpful. I may be able to use that for about half of the things I need to open the most recent (dated) workbook for. Other workbooks are saved in folders that may have other workbooks saved more recently but not the file name I need.

    Example, lets say my workbooks are titled "XXXXXX 2014.01.13" Is there any way to loop my code from the first post to search for "Now(), -2" and then "Now(), -3" and so on? So if I saved a workbook named "XXXXXX 2014.01.06" a week ago and now want to open that workbook can I search by the file name "XXXXXX" and open the last file with that name followed by the most recent date using this code or would I need something else?

    I would rather it pull the workbook based on the actual date that is saved in the filename rather than the last saved workbook of the folder.

    I do appreciate the help above, that will be useful for me.

    Thanks again.
    Last edited by richter394; 01-13-2014 at 09:08 PM. Reason: Better Clarification

+ 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. Excel slow to open from shortcut and/or recent docs
    By Steve Darwin in forum Excel General
    Replies: 0
    Last Post: 07-18-2013, 05:10 AM
  2. [SOLVED] vba loop to open new workbook and save them with a different name using master page
    By ankitparate in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-12-2013, 06:25 AM
  3. Replies: 2
    Last Post: 09-11-2012, 09:42 AM
  4. Macro to open most recent workbook
    By Xx7 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-10-2011, 02:08 AM
  5. open an exel workbook from my recent documents
    By Pat in forum Excel General
    Replies: 3
    Last Post: 07-04-2006, 11:50 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