I’ve recorded a macro to copy data from one workbook and paste the values into the workbook from which I initiate the macro. In the recorded version, I simply close the source workbook, but if there’s more than one file open the macro may not work as expected.
How do I get the code to return to the target workbook from which the macro is initiated?
The filename in the code ‘ Windows(“OT&TimeApprovalReport_2012-02-24.xlsm”).Activate will change daily.
Here’s what I’ve got so far, between recorded and edited code:
Sub GetTW_Data()
‘ GetTW_Data Macro
‘Opens source file (this filename never changes)
Workbooks.Open Filename:=”\lv10021financeDORDailyPayrolltw_daily.xls”
Windows(“tw_daily.xls”).Activate
‘Selects, then copies data from tw_daily
Range(“a1”).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
‘Return to target workbook
‘ ActiveWorkbook.Close
Range(“G7”).Select
ActiveSheet.PasteSpecial Paste:=xlPasteValues
‘ Windows(“OT&TimeApprovalReport_2012-02-24.xlsm”).Activate
End Sub