Excel 2010 vba 复制选择工作表,保存并关闭两个工作簿
Posted
技术标签:
【中文标题】Excel 2010 vba 复制选择工作表,保存并关闭两个工作簿【英文标题】:Excel 2010 vba copy select sheets, save and close both workbooks 【发布时间】:2013-11-08 15:04:22 【问题描述】:我有一个创建 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
【问题讨论】:
尝试关闭工作簿。从您的文字中,我了解到您正在按 F5 以查看宏是否有效。如果我这样做,我也会得到一个没有宏的空白宏窗口。但是,如果我关闭工作簿,我的 Workbook_BeforeClose 会按预期触发。 BTW @jacouh 是对的:您需要在BeforeClose
前面添加 Workbook_
才能在工作簿关闭时触发子触发器。
或者:如果您想在工作表打开的情况下测试代码,我认为您可以将(Cancel as Boolean)
注释掉,然后使用F5 进行测试。
这是我的最后一期。它现在像冠军一样开火。非常感谢您的帮助!
【参考方案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
【讨论】:
jaybee,知道了,谢谢,但是,当我尝试运行代码时,会弹出宏窗口并且我的宏不在列表中。如果我保存,窗口不会弹出,但代码永远不会触发,因为它永远不会命中我的断点。 正如@Jacouh 提到的***.com/a/19862953/1372610,您需要为 beforeClose 事件使用适当的命名约定。以上编辑。【参考方案2】:首先试试这个,将事件处理程序名称更改为:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
代替:
Private Sub BeforeClose(Cancel As Boolean)
【讨论】:
以上是关于Excel 2010 vba 复制选择工作表,保存并关闭两个工作簿的主要内容,如果未能解决你的问题,请参考以下文章
excel vba复制数据范围,打开新的xlsx文件重命名表并保存
如何使用vba在excel2010中将保存类型从excel工作簿更改为excel 97-2003?