VBA For Two Digit Year

Captain_Conman

Board Regular
Joined
Jun 14, 2018
Messages
54
Hi There,

I am essentially trying to open a workbook saved as today's date. "7-2-18.xlsx" I have written the follow code and it works perfectly. However, it is trying to reference a file with the year as "2018" instead of just "18". Any suggestions on how to make the macro reference "18" instead of "2018"?

Sub Test()
Dim M As Integer
Dim D As Integer
Dim Y As Integer


M = Month(Date)
D = Day(Date)
Y = Year(Date)


Application.Workbooks.Open Filename:="S:\SherwinWilliams\Oracle Reports" & M & "-" & D & "-" & Y & ".xlsx"


End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You could just use this:

Code:
Application.Workbooks.Open Filename:="S:\SherwinWilliams\Oracle Reports" & Format$(Date, "m-d-yy") & ".xlsx"
 
Upvote 0
RoryA,

This code worked great. My only question is why there is a "$" in the code? Is there significance with having it there? (Sorry, I am new to VBA and just curious!)
 
Upvote 0
There are two versions of many VBA functions. Format$ returns a String, whereas Format returns a Variant that contains a String. The former performs slightly better, so I tend to use it.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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