Opening Files Saved as Dates in VBA

trm85

New Member
Joined
Dec 4, 2015
Messages
1
I am working on a VBA code to open a password protected excel file. The file name is includes the days date in it, however the naming convention is not always consistent - Sometimes days or months may have a leading 0 while other times they will not. For example the file's name for today could potentially be "12-04-2015" or "12-4-2015".


Is there a code that would open the file if it is saved in either "MM-DD-YYYY" or "MM-D-YYYY" format? Maybe incorporating a wildcard?


The code I currently have is:


Workbooks.Open Filename:= _
"C:\Operations\Reports\Testing\" & Format(Date, "MM-DD-YYYY") & ".xlsx", Password:="test"


Thank you!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi and welcome to the forum.

Try something like this...

Code:
    [color=darkblue]Dim[/color] strPath [color=darkblue]As[/color] [color=darkblue]String[/color], strFile [color=darkblue]As[/color] [color=darkblue]String[/color]
    strPath = "C:\Operations\Reports\Testing\"
    strFile = Dir(strPath & Format(Date, "MM-DD-YYYY") & ".xlsx")
    [color=darkblue]If[/color] strFile = "" [color=darkblue]Then[/color] strFile = Dir(strPath & Format(Date, "MM-D-YYYY") & ".xlsx")
    [color=darkblue]If[/color] strFile = "" [color=darkblue]Then[/color] strFile = Dir(strPath & Format(Date, "M-DD-YYYY") & ".xlsx")
    [color=darkblue]If[/color] strFile = "" [color=darkblue]Then[/color] strFile = Dir(strPath & Format(Date, "M-D-YYYY") & ".xlsx")
    [color=darkblue]If[/color] strFile <> "" [color=darkblue]Then[/color]
        Workbooks.Open Filename:=strPath & strFile, Password:="test"
    [color=darkblue]Else[/color]
        MsgBox "Today's file not found. "
        [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,608
Members
449,038
Latest member
apwr

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