We help IT Professionals succeed at work.

Open File Using Macro

Ibukunkapital
on
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
 Range("J41").Select
 Range("I79:M79").Select
 Selection.Copy
 Windows("Bonds and Derivatives Flow PL Template.xlsm").Activate
 Range("I100").Select
 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
'
End Sub
Comment
Watch Question

Top Expert 2015
Commented:
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...

Saurabh...

Author

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:

Ibukun_10.03.2015
Ibukun_11.03.2015
Top Expert 2015

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

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