We help IT Professionals succeed at work.

Open File Using Macro

Please assist with this code. I am trying to record a macro to open file using filename that changes with date changes.

Sub CopyPriorBalance()
' CopyPriorBalance Macro
 Filepath = ("PriorFile")
 Filename = ("PriorFilename")
 Tabname = ("Daily, MTD, YTD")
 Workbooks.Open Filename:=Filename
 Sheets("Daily, MTD, YTD").Select
 Windows("Bonds and Derivatives Flow PL Template.xlsm").Activate
 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
Watch Question

Top Expert 2015
I'm not sure about how you define the file name as from the code you pasted it's not clear..

However in macro you can use the function of now() which basically gives you system date and time...

Now assuming you want to open a filename of yesterdaydate and save as like abc_03102015.xlsx

you can do this..

filename="abc_" & format(now()-1,"mmddyyyy") & ".xlsx"

Open in new window

This will automatically change basis of your system date when the time gets changed...



Could you assist with code that can open a file in a folder and the difference between the file names is the date at the back of the file names.

For example, the folder contains:

Top Expert 2015

This will become then...

for 10th mar...
filename="ibukun_" & format(now()-2,"dd.mm.yyyy") & ".xlsx"

for 11th mar...
filename="ibukun_" & format(now()-1,"dd.mm.yyyy") & ".xlsx"

Open in new window

Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.