Excel 2010 vba复制选择表,保存并关闭两个工作簿

时间:2013-11-08 15:04:22

标签: excel vba excel-vba

我有一个创建3个日志的工作簿。在关闭之前,我需要将这三张表复制到一个新工作簿中,然后我需要两个工作簿来保存和关闭自己。我已经得到了下面的代码,现在当我尝试运行模块时,它会调出宏窗口并且不会运行。我确实存储在ThisWorkbook中。任何意见都会非常感激。

                                                                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

2 个答案:

答案 0 :(得分:1)

新创建的工作簿将在以下后重点关注:

'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

会尝试隐藏工作表,但是你不能拥有没有可见工作表*的工作簿,所以你只需稍微重新编写你的代码就可以了。它可以工作。

Option Explicit

Private Sub Workbook_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

'Save new workbook
    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"
    activeworkbook.saveas(DstFile)
    activeworkbook.close true

'Hide Worksheets in original workbook
    DataWorkbook.Sheets("Service Change Log").Visible = False
    DataWorkbook.Sheets("Transaction Log").Visible = False
    DataWorkbook.Sheets("Call Initiation Log").Visible = False

'Close Worksheet
    DataWorkbook.Close Save:=True

End Sub

答案 1 :(得分:1)

首先尝试此操作,将事件处理程序名称更改为:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

而不是:

Private Sub BeforeClose(Cancel As Boolean)