gpt4 book ai didi

Excel VBA 自动化错误 : The object invoked has disconnected from its clients

转载 作者:行者123 更新时间:2023-12-01 18:35:07 25 4
gpt4 key购买 nike

我知道我以前见过对此问题的引用,但我已经尝试了一些建议,但仍然收到错误。我有一本工作簿,可以收集另一本书中的数据并生成报告。然后我想制作一个新的工作簿,将报告信息复制到新工作簿中,保存新工作簿并关闭它,然后继续处理下一个报告。应该执行此操作大约 10 次。在复制和粘贴工作表的代码部分中,出现错误

Error -2147417848 Automation error The object invoked has disconnected from its clients

我检查了有关此错误的其他帖子,并尝试了建议的解决方案,但没有任何结果。有趣的是,有时它会在中断之前完成 5 个代码周期,有时只有 2 个。唯一的一致性是它总是在同一个地方中断

fromBook.Sheets("Report").Copy Before:=newBook.Sheets("Sheet1")

我在模块顶部有“显式”选项,并且我已经检查过以确保它所侵入的子模块内部没有任何全局变量。话虽这么说,我完全有可能忽略了一些东西。我还在某个时刻放置了一个“计时器”,以确保 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

最佳答案

我在将 Excel 2000 转换为 2010 的多个项目中都遇到了这个问题。这是我发现的似乎有效的方法。我做了两处更改,但不确定哪一个导致了成功:

1) 我更改了关闭和保存文件的方式(从 close & save = true 更改为保存为相同的文件名并关闭文件:

...
Dim oFile As Object ' File being processed
...
[Where the error happens - where aArray(i) is just the name of an Excel.xlsb file]
Set oFile = GetObject(aArray(i))
...
'oFile.Close SaveChanges:=True - OLD CODE WHICH ERROR'D
'New Code
oFile.SaveAs Filename:=oFile.Name
oFile.Close SaveChanges:=False

2)我返回并查找代码中的所有 .range 并确保它是完整的构造..

Application.Workbooks("workbook name").Worksheets("worksheet name").Range("G19").Value

或者(不能100%确定这是否是正确的语法,但这是我所做的“努力”)

ActiveSheet.Range("A1").Select

关于Excel VBA 自动化错误 : The object invoked has disconnected from its clients,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17302918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com