插入复制的行会在Excel 2016中出现自动化错误

时间:2016-01-30 13:02:43

标签: vba excel-vba excel

在更新到Windows 10操作系统和MS Office 2016之前,我毫不费力地使用了以下代码 代码复制整个行,其中包含许多Excel内置公式,并在其下方插入整行。所有这些代码都在用户定义的函数中,当满足特定条件时,将从工作表selectionchange事件处理程序调用该函数。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If some condition = "" Then
        Block_AddRowEnd
    End If
End Sub

Sub Block_AddRowEnd()

    Dim StrEndRow As String
    Dim StrStartRow As String
    Dim Formula As String
    Dim NumberToReplaceStr As String
    Dim Str As String
    Dim Index As Integer

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    'User defined funtion to get the end row
    EndRow = Block_EndRow()

    If Not EndRow = 0 Then
        'User defined funtion to get the Start row
        StartRow = Block_StartRow()

        If Not StartRow = 0 Then
            StrEndRow = EndRow - 1
            StrEndRow = StrEndRow + ":" + StrEndRow

            Rows(StrEndRow).Select
            Selection.Copy

            StrEndRow = EndRow
            StrEndRow = StrEndRow + ":" + StrEndRow

            Rows(StrEndRow).Select
            Selection.Insert Shift:=xlDown

            '****The above Statement gives the following Error
            '"Error -2147417848 (&H80010108): The object invoked 
            'has disconnected from its clients."
            'This error is random and not reproducible at all 
            'the times *******"

            Application.CutCopyMode = False

            'Other code follows here
        End if
    End if
End Sub

0 个答案:

没有答案