Formatting Dates in VB Code.

tigrou

New Member
Joined
Jan 24, 2003
Messages
24
Hello, can anyone help me with this one please. I have the following code to open up an excel workbook. For example the filepath and file name is - 'N:\Group\FTG Performance\FTG Performance 2003\may2003\ftg21may.xls'
The workbook name changes daily according to the date e.g. 'ftg02jan.xls', 'ftg12sep.xls' and so on. The workbooks are stored in sub folders according to the month of the year to which they relate e.g. 'ftg21feb.xls' in sub folder 'february2003' and so on. This is the VB code -

Dim C
C = "G:\Group\FTG Performance\FTG Performance 2003\may2003\"
Dim D As String
Dim M
M = "may"
Dim E
E = ".xls"
Dim G
G = "\ftg"
Dim FName As String
D = Format(Day(Now - 1), "00")
Dim WB As Workbook
Set WB = Workbooks.Open(Filename:=C & G & D & "" & M & E)
Windows(WB.Name).Activate

It works but every so often I have to edit the code so it opens the correct monthly sub folder e.g. '\may2003\' and the variable M="may" in these specific date formats. I wonder if anyone can help me to automate this please. I've tried to format the MONTH(NOW) but it keeps reverting to 'January'. Thanks in anticipation.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,221,709
Messages
6,161,432
Members
451,705
Latest member
Priti_190

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