vba 错误 -2147417848 自动化错误调用的对象已与其客户端断开连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13720053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:43:07  来源:igfitidea点击:

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

excel-vbaruntime-errorexcel-2010vbaexcel

提问by hiba.awar

I am working on clearing some tables in 3 excel sheets.

我正在清除 3 个 excel 表中的一些表格。

My code was working fine with Excel 2007, as i switched to excel 2010 i started getting this runtime error Automation error The object invoked has disconnected from its clients, error number -2147417848and then excel freezes.

我的代码在 Excel 2007 上运行良好,当我切换到 excel 2010 时,我开始收到此运行时错误自动化错误调用的对象已与其客户端断开连接,错误号为-2147417848,然后 excel 冻结。

the error is showing after running this function 2 times on the line

在线运行此功能2次后显示错误

    Sheets("Risk Details").Range("A3" & ":BV" & (count + 1)).Delete Shift:=xlUp

can anyone help me in finding out why this is happening and how to work around it?

谁能帮我找出为什么会发生这种情况以及如何解决它?

below are the full code for the function

以下是该函数的完整代码

Function clearData(Optional refresh As Boolean) As Boolean
Application.ScreenUpdating = False

Sheets("Single Risk Register").Unprotect myPass
Sheets("Single Risk Register").Activate
Cells.FormatConditions.Delete


Sheets("Risk Details").Unprotect myPass
Sheets("Risks").Unprotect myPass
Dim currentrange As Range


Sheets("Risks").Activate
Set currentrange = Sheets("Risks").Range("RisksTable")
currentrange.ClearContents


Dim count As Integer
count = Sheets("Risks").ListObjects("RisksTable").ListRows.count
If count > 1 Then
    Sheets("Risks").Range("A3" & ":C" & (count + 1)).Delete Shift:=xlUp
End If


Sheets("Risk Details").Activate
Set currentrange = Sheets("Risk Details").Range("RiskDetails")
currentrange.ClearContents
count = Sheets("Risk Details").ListObjects("RiskDetails").ListRows.count
If count > 1 Then
    Sheets("Risk Details").Range("A3" & ":BV" & (count + 1)).Delete Shift:=xlUp
End If
Sheets("Single Risk Register").Activate
count = CInt(Range("ActionsCount"))
Range("ActionsCount") = 1

Dim tableLastRow As Integer
tableLastRow = getLastTableRow()
If (tableLastRow >= 48) Then
    Sheets("Single Risk Register").Range("B48" & ":K" & tableLastRow).Delete Shift:=xlUp
End If
Range("ActionsTable[[Action Description]:[Action Commentary]]").Select
Application.CutCopyMode = False
Selection.ClearContents

Application.ScreenUpdating = False
Dim DataRange As Range

Dim o As Shape
Dim tC As Variant
Dim propertiesCount As Integer
Dim i As Integer

tC = Sheets("TableColumns").ListObjects("TableColumns").DataBodyRange

propertiesCount = GetTemplatePropertiesCount(templates(0)) - 1
ReDim properties(0 To propertiesCount - 1, 0 To 4)
If (properitesColl.Item(i + 1)(propertiesCount, 2) = templates(0)) Then
    properties = properitesColl.Item(i + 1)
End If

For count = 0 To propertiesCount - 1
    If ((properties(count, 4) <> "C25") And (properties(count, 4) <> "C26") And (properties(count, 4) <> "C27") And (properties(count, 4) <> "C28") And (properties(count, 4) <> "C38") And (properties(count, 4) <> "C39") And (properties(count, 4) <> "C40") And (properties(count, 4) <> "C41"))

    Then
        Sheets("Single Risk Register").Range(properties(count, 4)) = ""
    End If
Next

Dim columnCount As Integer
columnCount = GetColumnCount()
count = 0
For i = 1 To columnCount
    If tC(i, 4) <> "Action" Then
        Set currentrange = Range(tC(i, 4))
    Else
        Set currentrange = Range("ActionsTable[" & tC(i, 1) & "]")
    End If
    If ((tC(i, 4) = "C25") Or (tC(i, 4) = "C26") Or (tC(i, 4) = "C27") Or (tC(i, 4) = "C28") Or (tC(i, 4) = "C38") Or (tC(i, 4) = "C39") Or (tC(i, 4) = "C40") Or (tC(i, 4) = "C41")) 

     Then
        With currentrange.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 15654866
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With

    Else
        With currentrange.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent1
            .TintAndShade = 0.799981688894314
            .PatternTintAndShade = 0
        End With
    End If
Next


Sheets("Single Risk Register").Unprotect myPass
If (Range("C3").Value = "") Then
    Range("ActionsTable[[Action Description]:[" & addTitle & "]]").Select
    Selection.Locked = True
    Rows("45:47").Select
    Selection.EntireRow.Hidden = True
End If


Sheets("Single Risk Register").Range(Range("TitleCell")).Value = Range("OldTitle") + " (" + Range("Project_Title") + ")"

Sheets("Single Risk Register").Protect myPass, AllowFiltering:=True
Sheets("Risk Details").Protect myPass, AllowFiltering:=True
Sheets("Risks").Protect myPass, AllowFiltering:=True

doNotRun = False
Range("C3").Select
Application.ScreenUpdating = True

Exit Function

err:  

Sheets("Risk Details").Protect myPass
Sheets("Risks").Protect myPass
MsgBox err.Description, vbCritical, "Error"

回答by JustinJDavies

Microsoft provide a high-level answer:

微软提供了一个高级答案

If the code always appears to work the first time that it is run, and if the errors or the unexpected behaviors occur only during subsequent calls to the same code, an unqualified method call is the cause.

如果代码在第一次运行时似乎总是有效,并且如果错误或意外行为仅在对同一代码的后续调用期间发生,则原因是不合格的方法调用。