Macro to open filename that changes daily...

j844929

Active Member
Joined
Aug 18, 2002
Messages
423
Hi,

Is it possible to create a button in a spreadsheet that can open a file where the date in the filename changes each day?

E.g. the file is called tradesheet200802Germany.xls and then the next will be tradesheet210802Germany.xls

Also, I'd like to know how to create a macro that saves a file in a specific location with the date contained in the file.

Hope you can help,

Kind regards,

Tim Pickop
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi Tim


Sub File_Open()
Dim OldDate As Date
Dim OldFile, OldPath, NewFile, NewPath As String
Dim WBook As Workbook
Dim OldDay, OldMonth, OldYear, NewDay, NewMonth, NewYear As String

'To open the file name with previous day's date

OldDate = Now - 1
OldPath = "D:"
OldDay = Day(OldDate)
OldMonth = Month(OldDate)
OldYear = Year(OldDate)
If Len(OldDay)< 2 Then OldDay = "0" & OldDay
If Len(OldMonth)< 2 Then OldMonth = "0" & OldMonth
OldYear = Right(OldYear, 2)

OldFile = OldPath & "tradesheet" & OldDay & OldMonth & OldYear & "Germany.xls"

Set WBook = Workbooks.Open(OldFile)

'Here write code to do changes in the workbook

'To save in the same location and same filename copy the following.
WBook.Save
WBook.Close

'To save in different location with todays date in filename copy the following
NewDate = Now
NewPath = "D:My Documents"
NewDay = Day(NewDate)
NewMonth = Month(NewDate)
NewYear = Year(NewDate)
If Len(NewDay)< 2 Then NewDay = "0" & NewDay
If Len(NewMonth)< 2 Then NewMonth = "0" & NewMonth
NewYear = Right(NewYear, 2)
NewFile = NewPath & "tradesheet" & NewDay & NewMonth & NewYear & "Germany.xls"
WBook.SaveAs (NewFile)
WBook.Close

End Sub

You can assign this macro to a button and place it in your worksheet.
This message was edited by gnaga on 2002-09-06 05:38
 
Upvote 0

Forum statistics

Threads
1,218,923
Messages
6,145,247
Members
450,604
Latest member
ericmwr

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