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
Who is Participating?
Saurabh Singh TeotiaCommented:
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...

IbukunkapitalAuthor Commented:
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:

Saurabh Singh TeotiaCommented:
This will become then...

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

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

Open in new window

Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.