Open file with variable date

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
296
Office Version
  1. 2016
Platform
  1. Windows
All,

I am trying to use a macro that will open a file in a folder called "DOR". This file could exist in that folder or a subfolder (DOR/Archived) or in a monthly folder, that changes each month (2022.11) or (2022.12) and so on.

The filename will be in the same format every day, "DO Report 11-09-22". So tomorrow the filename will be "DO Report 11-10-22". I need to open this file no matter what subfolder of the main DOR folder it is in.

The macro has to open YESTERDAY'S file. So when I click the macro button the code will open yesterdays file.

This is what I have so far. I really don't need the msg box but if it stays I'm ok with it.

VBA Code:
Sub OpenWorkbook()
    Dim sYesterday As String, sFileName As String

    sYesterday = Format(Now - 1, "MM-DD-YY")
    MsgBox sYesterday
    
    sFileName = "https://airport.ishare.tsa.dhs.gov/fieldlocations/MHT/soc/SOC Scheduling/Shared Documents/DOR/DO Report " & sYesterday & ".xlsm"
    
    MsgBox sFileName
    
End Sub

The only thing I get is the msg box but the file doesn't open. Like I said, I don't need the msg box, I just need yesterdays file to open no matter what subfolder it is in. Thanks in advance.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Your macro is missing an 'Open' statement, so it won't open the file. You need to add Workbooks.Open sFileName.

Not tested.
VBA Code:
Sub OpenWorkbook()
    Dim sYesterday As String, sFileName As String

    sYesterday = Format(Now - 1, "MM-DD-YY")
    MsgBox sYesterday
    
    sFileName = "https://airport.ishare.tsa.dhs.gov/fieldlocations/MHT/soc/SOC Scheduling/Shared Documents/DOR/DO Report " & sYesterday & ".xlsm"
    
    Workbooks.Open sFileName
    
    MsgBox sFileName
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top