如何关闭工作簿?

时间:2019-04-15 14:55:08

标签: excel vba

我想关闭Excel工作簿,但我不能,它给我运行时错误1004

我已经尝试了以下代码中注释部分的所有内容。

Private Sub April()

'open workbook
Dim fileName As String
Dim directory As String
Dim fileNameLong As String
Dim rpr As Workbook

directory = "C:\Users\.........\Desktop\Rapor"
fileName = dir("C:\Users\.........\Desktop\Rapor" & "\*.xlsm")
fileNameLong = directory & "\" & fileName
Set rpr = Workbooks.Open(fileNameLong)

Workbooks(2).Worksheets(1).Range("bb2").Copy
Workbooks(1).Worksheets(1).Range("a2").PasteSpecial xlPasteValues
Workbooks(2).Worksheets(1).Range("g3").Copy
Workbooks(1).Worksheets(1).Range("b2").PasteSpecial xlPasteValues
Workbooks(2).Worksheets(1).Range("g5").Copy
Workbooks(1).Worksheets(1).Range("c2").PasteSpecial xlPasteValues
Workbooks(2).Worksheets(1).Range("g21:be21").Copy
Workbooks(1).Worksheets(1).Range("d2").PasteSpecial xlPasteValues
Application.CutCopyMode = False

'save the file to archive later but first to close the excel file. 

Dim saveNameLong As String
saveNameLong = directory & "\Archive\" & fileName
Application.Workbooks(1).Activate
Range("a2").Select
rpr.Close False
'Application.Workbook(ActiveWorkbook.FullName).Close
'ActiveWorkbook.Close
'Application.Workbook.Close False, fileName
'Application.Workbooks(2).Close
'Name fileNameLong As saveNameLong

End Sub

运行时错误1004。我无法关闭excel工作簿。

编辑: Ahmed AU ..是的,我要关闭的工作簿中有一个Private Sub Workbook_BeforeClose(Cancel As Boolean)。这有问题吗?我该如何解决?

编辑(已解决): 我刚刚添加了application.events = false 而且有效!!

0 个答案:

没有答案