Hi, please could someone help with the following code (it is quite simple/basic):
---
Sub GetSensData()
Dim rng As Range
Dim Mail_Date As String
Mail_Date = Range("cobdate")
'Open a workbook
'Open method requires full file path to be referenced.
Workbooks.Open "\\sirius.ants.ad.anplc.co.uk\tradmktrisk\00 - Core Risk Team\Reporting\SNP Rates\P&L\" & Format(Range("cobdate"), "yyyy") & "\" & Format(Range("cobdate"), "mm Mmmm") & "\SNP Rates P&L " & Format(Range("cobdate"), "yyyy mm dd") & " (old vesion).xlsm"
'Run the Full Daily Process macro in Old Version to be copied into New Version
Application.Run "'SNP Rates P&L " & Format(Range("cobdate"), "yyyy mm dd") & " (old vesion).xlsm'!fullDailyProcess"
'Message Box to say 'Done' when ran Macro
MsgBox ("Old Version Macro ran!")
'Copy the IR Delta sheet from Old Version into New Version
Workbooks("SNP Rates P&L 2020 10 01 (old vesion).xlsm").Worksheets("IR Delta").Range("A1:AL9").Copy _
Workbooks("20201001_SNP_Rates_PNL MR.xlsm").Worksheets("IR Delta").Range("A1")
'Worksheets("IR Delta").Activate
'Copy the XCCY Delta sheet from Old Version into New Version
Workbooks("SNP Rates P&L 2020 10 01 (old vesion).xlsm").Worksheets("XCCY Delta").Range("A1:AL7").Copy _
Workbooks("20201001_SNP_Rates_PNL MR.xlsm").Worksheets("XCCY Delta").Range("A1")
Worksheets("XCCY Delta").Activate
'Save and close old version
Workbooks("SNP Rates P&L 2020 10 01 (old vesion).xlsm").Close SaveChanges:=True
End Sub
- The code is essentially opening a workbook (which is rolled over daily) - this is a daily file which therefore has a new name daily varying by latest close of business date (i.e. workday - 1)
- Running (calling) a Macro within that workbook - the Macro name also varies daily by latest close of business date
- Copying data from two tabs (namely "IR Delta" and "XCCY Delta" below) after that Macro has run into the current workbook
- Closing and saving that workbook accordingly
- The code works fine for the current date, however, I need to copy and paste each file daily and roll the date over for each close of business date. Thus, the date at the end of the file name varies
- When I have tried to add "Format(Range("cobdate"), "yyyy mm dd")" to wherever in the file paths is needed, I get an error message that the 'Subscript is out of range'
- How do I make the below dates variable accordingly? it works for the 'Workbooks.Open' line of code, but not for any of the ones after
---
Sub GetSensData()
Dim rng As Range
Dim Mail_Date As String
Mail_Date = Range("cobdate")
'Open a workbook
'Open method requires full file path to be referenced.
Workbooks.Open "\\sirius.ants.ad.anplc.co.uk\tradmktrisk\00 - Core Risk Team\Reporting\SNP Rates\P&L\" & Format(Range("cobdate"), "yyyy") & "\" & Format(Range("cobdate"), "mm Mmmm") & "\SNP Rates P&L " & Format(Range("cobdate"), "yyyy mm dd") & " (old vesion).xlsm"
'Run the Full Daily Process macro in Old Version to be copied into New Version
Application.Run "'SNP Rates P&L " & Format(Range("cobdate"), "yyyy mm dd") & " (old vesion).xlsm'!fullDailyProcess"
'Message Box to say 'Done' when ran Macro
MsgBox ("Old Version Macro ran!")
'Copy the IR Delta sheet from Old Version into New Version
Workbooks("SNP Rates P&L 2020 10 01 (old vesion).xlsm").Worksheets("IR Delta").Range("A1:AL9").Copy _
Workbooks("20201001_SNP_Rates_PNL MR.xlsm").Worksheets("IR Delta").Range("A1")
'Worksheets("IR Delta").Activate
'Copy the XCCY Delta sheet from Old Version into New Version
Workbooks("SNP Rates P&L 2020 10 01 (old vesion).xlsm").Worksheets("XCCY Delta").Range("A1:AL7").Copy _
Workbooks("20201001_SNP_Rates_PNL MR.xlsm").Worksheets("XCCY Delta").Range("A1")
Worksheets("XCCY Delta").Activate
'Save and close old version
Workbooks("SNP Rates P&L 2020 10 01 (old vesion).xlsm").Close SaveChanges:=True
End Sub