Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 随机';1004应用程序定义或对象定义错误'/';1004范围类的特殊方法失败';_Vba_Excel - Fatal编程技术网

Vba 随机';1004应用程序定义或对象定义错误'/';1004范围类的特殊方法失败';

Vba 随机';1004应用程序定义或对象定义错误'/';1004范围类的特殊方法失败';,vba,excel,Vba,Excel,宏(不是我的,我“继承”了它)运行多个循环。代码有几千行,所以我只提供代码片段中循环的不稳定部分 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, C

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

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
)成功地应用于宏的其他部分(类似的范围、类似的工作表、类似的数据类型),但它始终仅在整个宏的这一行中断

我认为buggy部分的解决方案如下:

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 method of Range类失败”(当然,一段时间后,最初它也可以正常工作)。也试过
.Value2=.Value2
,但都是一样的废话

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

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

另外,我意识到代码可能没有得到优化(例如,你可能会选择
工作表。激活所用的
方法),但是,同样,这是给我的工作,我不想重写代码,至少现在,除非有必要使其工作

编辑

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

  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上


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

您正确地引用了父工作表

With ActiveSheet
    repoLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
为什么停在那里?是您分配(和维护)父工作表的最大朋友

行数和列数分配应始终为
,而不是
整数
,无论整数听起来有多“酷”

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

现在您可能会注意到,编码的部分不显示粗体。哪一行产生错误?编辑器将其标记为**。我的意思是:
Range(Cells(repoLastRow+1,3),Cells(repoLastRow+1,repoLastCol)).Value=\Range(Cells(repoLastRow+1,3),Cells(repoLastRow+1,repoLastCol)).Value
相关主题:您是否返回带有
VLOOKUP(&column\u letter&“1&&column\u letter&”的字符串或数字2,…
?如果您希望输入一个字符串,并且在输入空白时不希望输入零,则有更好的方法,但如果您希望返回一个数字,则不建议使用此方法。如果您转到“ATT_LEV*工作表名称”选项卡并右键单击► 查看代码,工作表的代码表是否有任何事件宏;特别是工作表更改?此工作簿是否有任何事件宏;特别是工作表更改?我将使用此vlookup返回一个数字或一个空白。为此,我个人强烈主张使用with…End with,如果我们你是这个工具的原始开发者。不仅仅是关于这段代码,还有更多的代码可以使用这个子句,但是对我来说这样做太费时了。关于行/列编号,我会尝试
Long
,但是如果行/列不在
整数范围之外,那又有什么关系呢e?这无关紧要,如果是,错误也会有所不同。这只是一件你永远不必担心的事情,除非你在访问超出整数范围的行时有意抛出一个erro。同意这一点,这将是一个溢出EROR。但是,在我的宏中,当前的
repoLastRow
是804和e> repoLastCol
是1667,所以前面还有很多空间(循环的每一次迭代行都会增加1,具体取决于列)。此外,范围始终是一维数组,从第3列到1667列,所以不明白为什么要让Excel在这一列上阻塞。。。