I have a workbook that creates 3 logs. Before close I need those three sheets to copy into a new workbook then I need both workbooks to save and close themselves. I've gotten as far as the code below and now when I try to run the module it brings up the macro window and won't run. I do have this stored in ThisWorkbook. Any input would be much appreciated.
Mary
Option Explicit
Private Sub BeforeClose(Cancel As Boolean)
'Save worksheets to new workbook
Dim strSaveName As String
Dim DstFile As String 'destination File name
Dim DataWorkbook As Workbook
'Unhide Worksheets
Set DataWorkbook = ActiveWorkbook
DataWorkbook.Sheets("Service Change Log").Visible = True
DataWorkbook.Sheets("Transaction Log").Visible = True
DataWorkbook.Sheets("Call Initiation Log").Visible = True
ChDir "P:\CSR Rollback Tool\Test"
strSaveName = Replace(Application.UserName, ",", "")
'Copy Sheets
Sheets(Array("Service Change Log", "Transaction Log", "Call Initiation Log")).Copy
'Hide Worksheets
Sheets("Service Change Log").Visible = False
Sheets("Transaction Log").Visible = False
Sheets("Call Initiation Log").Visible = False
'Close Worksheet
MsgBox "P:\CSR Rollback Tool\" & strSaveName & Format(Now(), "yyyymmdd hh-mm") & ".xlsx"
Application.DisplayAlerts = False
DstFile = "P:\CSR Rollback Tool\test\" & strSaveName & Format(Now(), "yyyymmdd hh-mm") & ".xlsx"
Workbooks(DstFile).Close SaveChanges:=True
DataWorkbook.Close Save:=True
End Sub
Workbook_
in front ofBeforeClose
to have the sub trigger when the workbook closes. – Karl Rookey Nov 8 '13 at 17:01(Cancel as Boolean)
and then test it using F5. – Karl Rookey Nov 8 '13 at 17:06