我知道我之前已经看到过对这个问题的引用,但我已经尝试了一些建议,但我仍然收到错误。我有一本从另一本书收集数据并生成报告的工作簿。然后我想制作一个新工作簿,将报表信息复制到新簿中,保存新簿并关闭它,然后继续下一个报表。它应该这样做大约 10 次。在我复制和粘贴工作表的部分代码中,出现错误
错误 -2147417848 自动化错误 调用的对象已与其客户端断开连接
我检查了有关此错误的其他帖子,并尝试了建议的解决方案,但没有任何结果。有趣的是,有时它会在中断之前完成 5 个代码循环,有时只有 2 个。唯一的一致性是它总是在同一个地方中断
fromBook.Sheets("Report").Copy Before:=newBook.Sheets("Sheet1")
我在模块顶部有 Explicit 选项,并且我已经检查以确保它正在进入的 sub 内部没有任何全局变量。话虽如此,我完全有可能忽略了一些东西。我还设置了一个“计时器”,以确保 excel 表不会相互重叠。
我真的可以使用帮助!
这是我的子代码:
Sub CreateAndSave(ByRef Reg As Integer, ByVal j As Integer)
Dim fromBook As Workbook
Dim fromSheet As Worksheet
Dim newBook As Workbook
Dim fileExists As Boolean
Dim i As Integer
Dim Holder As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set fromBook = Application.Workbooks("Region_Audit_Report")
Set newBook = Workbooks.Add
With newBook
.SaveAs Filename:="G:\DataTeam\ExcelDev\Audit Report\Region Workbooks\Region" & Reg & " " & Month(Date) & "-" & Day(Date) & "-" & Year(Date) & ".xlsx" _
, FileFormat:=xlOpenXMLWorkbook
End With
Set newBook = Application.Workbooks("Region" & Reg & " " & Month(Date) & "-" & Day(Date) & "-" & Year(Date) & ".xlsx")
fromBook.Sheets("Report").Copy Before:=newBook.Sheets("Sheet1")
fromBook.Sheets("MonthData").Copy After:=newBook.Sheets("Report")
newBook.Sheets("MonthData").Range("A1") = "Month"
newBook.Sheets("MonthData").Range("B1") = "Store#"
newBook.Sheets("MonthData").Range("C1") = "District"
newBook.Sheets("MonthData").Range("D1") = "Region"
newBook.Sheets("MonthData").Range("E1") = "Due Date"
newBook.Sheets("MonthData").Range("F1") = "Comp Date"
newBook.Sheets("MonthData").Range("G1") = "# of Errors"
newBook.Sheets("MonthData").Range("H1") = "Late?"
newBook.Sheets("MonthData").Range("I1") = "Complete?"
newBook.Sheets("MonthData").Range("A1:I1").Interior.ColorIndex = 43
newBook.Save
newBook.Close
Application.DisplayAlerts = True
End Sub