Consulting

Results 1 to 2 of 2

Thread: VBA - Opening File with Yesterday's Date

  1. #1

    VBA - Opening File with Yesterday's Date

    I hope everyone is doing ok with COVID and keeping busy.
    I was trying to make a macro in order to open files based on yetserday's date.
    For instance, everyday, there is file that is created.
    I just wanted to open the file based on yesterday's date.
    I looked extensively on-line, youtube, and more for information, but I just couldn't solve the problem. Below is what I am trying to do:

    Sub OpenWorkbook()
    '
    ' Code to open Excel File based on Shorts Report with yesterday's date

    ChDir "W:\Purchasing & Inventory\Daily Shorts Report"
    Workbooks.Open Filename:= _
    "W:\Purchasing & Inventory\Daily Shorts Report\Daily Shorts Report "& Format(NOW-1(),"YYYYMMDD")&".xlsx")

    Would someone know how to provide me some advice?
    Thank you

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Try this

    Remove MsgBox's or comment out and uncomment the .Open line


    Option Explicit
    
    
    ' Code to open Excel File based on Shorts Report with yesterday's date
    Sub OpenWorkbook()
        Dim sYesterday As String, sFileName As String
    
    
        sYesterday = Format(Now - 1, "YYYYMMDD")
        MsgBox sYesterday
        
        sFileName = "W:\Purchasing & Inventory\Daily Shorts Report\Daily Shorts Report " & sYesterday & ".xlsx"
        
        MsgBox sFileName
        
    '    Workbooks.Open sFileName
        
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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