随机'1004应用程序定义或对象定义的错误'/'1004 Range类的PasteSpecial方法失败'

时间:2015-11-06 11:30:34

标签: excel vba excel-vba

宏(不是我的,我“继承”它)运行多个循环。代码有几千行,所以我只在代码片段中提供循环中不稳定的部分。

Dim repoLastRow As Integer, repoLastCol As Integer
Worksheets("ATT_LEV").Activate
With ActiveSheet
    repoLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
repoLastCol = ActiveSheet.Cells(3, Columns.Count).End(xlToLeft).Column
Cells(repoLastRow + 1, 1).Value = xmlAgreement1
Cells(repoLastRow + 1, 2).Value = repoLastRow + 1
Cells(repoLastRow + 1, 5).Value = pubCurrCNID

Cells(repoLastRow + 1, 4).Formula = "=IF(IFERROR(FIND(""MASTER"",'Import xml 0'!A2,1),0)>0,""MASTER"",IF(IFERROR(FIND(""ANNEX"",'Import xml 0'!A2,1),0)>0,""ANNEX"",""""))"
Cells(repoLastRow + 1, 4).Value = Cells(repoLastRow + 1, 4).Value

Range(Cells(repoLastRow + 1, 3), Cells(repoLastRow + 1, repoLastCol)).Dirty
For i = 5 To repoLastCol
    column_letter = Split(Columns(i + 1).Address, ":")(0)
    Cells(repoLastRow + 1, i + 1).Formula = "=IFERROR(IF(VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,9,FALSE)=0,"""",VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,8,FALSE)),"""")"
    Cells(repoLastRow + 1, i + 2).Formula = "=IFERROR(IF(VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,9,FALSE)=0,"""",VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,9,FALSE)),"""")"
    i = i + 1
Next i
Range(Cells(repoLastRow + 1, 3), Cells(repoLastRow + 1, 3)).Formula = "=CompareSingle!C1"

Range(Cells(repoLastRow + 1, 3), Cells(repoLastRow + 1, repoLastCol)).Value = _
Range(Cells(repoLastRow + 1, 3), Cells(repoLastRow + 1, repoLastCol)).Value

这是崩溃的具体部分。

Range(Cells(repoLastRow + 1, 3), Cells(repoLastRow + 1, repoLastCol)).Value = _
Range(Cells(repoLastRow + 1, 3), Cells(repoLastRow + 1, repoLastCol)).Value

问题是这个特定的行给了'1004应用程序定义的或对象定义的错误'但是只有在循环的几次迭代之后,让我们说在40之后。所以对于第41次宏来说这个部分代码,它只是打破。它有时几乎完全循环50次,但最终它总会崩溃 - 我从来没有超过50次循环。有时它会以完整的Excel冻结结束,但更多时候它只是一个调试器弹出窗口。有趣的是,如果我停止宏并从它崩溃的循环开始它,它将顺利通过这个声明,并在另外几十个段落后再次破坏。然而,最有趣的是,它总是在整个宏中打破这一行,尽管类似的模式(.value=.value)成功应用于宏的其他部分(类似范围,类似的工作表,类似的数据类型)

我认为有缺陷部分的解决方法如下:

Range(Cells(repoLastRow + 1, 3), Cells(repoLastRow + 1, repoLastCol)).Copy
Range(Cells(repoLastRow + 1, 3), Cells(repoLastRow + 1, 3)).PasteSpecial xlPasteValues
Application.CutCopyMode = False

但相反,我收到'1004 PasteSpecial方法的Range类失败'(当然,在一段时间之后,它最初也能正常工作)。还试过.Value2=.Value2,但同样废话。

我提到在停止宏并从最后一个正确的循环重新运行它之后就可以了。这是真的,但在此行崩溃之后,Excel通常会变得对VBA调用没有反应,我只能在保存,退出并重新打开工作表后才能继续。例如,Worksheet.Activate方法无效(没有任何反应,被调用的工作表不会被激活)或Cells.Clear也不起作用并呈现错误。重新打开工作簿后,一切都恢复正常,我可以运行该程序。如果重要,宏存储在.xlsb中,是在Excel 2010中创建并运行的(最初位于.xlsm)。

任何人都可能知道为什么会这样?为什么随意?

PS。我意识到代码可能没有被优化(例如,您可能会选择使用的Worsksheet.Activate方法)但是,再次,这是......我已经被允许使用并且我不想重写代码,至少目前,除非有必要使其发挥作用。

修改

我已经解决了我的问题,至少部分解决了。我所做的是在用公式填充后立即将单元格转换为值,现在我可以永远循环:

  For i = 5 To repoLastCol
        column_letter = Split(Columns(i + 1).Address, ":")(0)
        Cells(repoLastRow + 1, i + 1).Formula = "=IFERROR(IF(VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,9,FALSE)=0,"""",VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,8,FALSE)),"""")"
        Cells(repoLastRow + 1, i + 1).Value = Cells(repoLastRow + 1, i + 1).Value
        Cells(repoLastRow + 1, i + 2).Formula = "=IFERROR(IF(VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,9,FALSE)=0,"""",VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,9,FALSE)),"""")"
        Cells(repoLastRow + 1, i + 2).Value = Cells(repoLastRow + 1, i + 2).Value
        i = i + 1
    Next i

我对此解决方法并不完全满意,因为它仍然无法解释导致错误的原因。您可能会觉得它很有趣,但在最后几次运行中,错误发生在第40个循环完全(当然总是在同一行)。当excel崩溃时(在某些情况下)我尝试使用Visual Studio调试它,并且一旦获得有关VBE7.dll问题的信息。

对问题的性质有任何猜测吗?

1 个答案:

答案 0 :(得分:0)

您正在使用

正确引用父工作表
With ActiveSheet
    repoLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

为何停在那里? With ... End With statement是您分配(和维护)父工作表的最大朋友。

行号和列号分配应始终为Long,而不是Integer,无论“整齐”的声音有多“冷”。

Dim repoLastRow As Long, repoLastCol As Long

With Worksheets("ATT_LEV")
    .Activate
    repoLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    repoLastCol = .Cells(3, Columns.Count).End(xlToLeft).Column

    .Cells(repoLastRow + 1, 1).Value = xmlAgreement1
    .Cells(repoLastRow + 1, 2).Value = repoLastRow + 1
    .Cells(repoLastRow + 1, 5).Value = pubCurrCNID

    .Cells(repoLastRow + 1, 4).Formula = "=IF(IFERROR(FIND(""MASTER"",'Import xml 0'!A2,1),0)>0,""MASTER"",IF(IFERROR(FIND(""ANNEX"",'Import xml 0'!A2,1),0)>0,""ANNEX"",""""))"
    .Cells(repoLastRow + 1, 4).Value = Cells(repoLastRow + 1, 4).Value

    .Range(.Cells(repoLastRow + 1, 3), .Cells(repoLastRow + 1, repoLastCol)).Dirty
    For i = 5 To repoLastCol
        column_letter = Split(.Columns(i + 1).Address, ":")(0)
        .Cells(repoLastRow + 1, i + 1).Formula = "=IFERROR(IF(VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,9,FALSE)=0,"""",VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,8,FALSE)),"""")"
        .Cells(repoLastRow + 1, i + 2).Formula = "=IFERROR(IF(VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,9,FALSE)=0,"""",VLOOKUP(" & column_letter & "1&" & column_letter & "2,CompareSingle!$A:$I,9,FALSE)),"""")"
        i = i + 1
    Next i
    .Range(.Cells(repoLastRow + 1, 3), .Cells(repoLastRow + 1, 3)).Formula = "=CompareSingle!C1"

    .Range(.Cells(repoLastRow + 1, 3), .Cells(repoLastRow + 1, repoLastCol)) = _
        .Range(.Cells(repoLastRow + 1, 3), .Cells(repoLastRow + 1, repoLastCol)).Value
End With