2015-11-06 101 views
0

一個宏(不是我的,但我「繼承」它)運行多個循環。該代碼有幾千行,所以我只提供片段中循環的部分。隨機'1004應用程序定義或對象定義的錯誤'/'1004範圍類的PasteSpecial方法失敗'

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 Range類的PasteSpecial方法失敗」(當然,一段時間後,開始它的工作原理確定太)。也試過.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的問題信息。

有關問題性質的任何猜測?

+0

您現在可能會注意到,編碼部分不顯示粗體。哪一行產生錯誤? – Jeeped

+0

編輯器用**標記它。我的意思是:Range(Cells(repoLastRow + 1,3),Cells(repoLastRow + 1,repoLastCol))。Value = _ 範圍(Cells(repoLastRow + 1,3),Cells(repoLastRow + 1,repoLastCol ))。值' –

+0

相關主題:您是否正在返回一個字符串或一個包含該'VLOOKUP(「&column_letter&」1&「&column_letter&」2,...'的數字?如果您期待一個字符串並且不想要一個零時,當你打一個空白有一個更好的方法,但不建議如果你期望一個數字返回。 – Jeeped

回答

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 
+0

感謝你們,我個人是'With With End With'的堅定支持者,如果我是這個工具的原始開發者,我會使用它。這不僅是關於這個特定的代碼塊,還有更多的可以使用這個子句,但這對我來說太耗時了。至於行/列號,我會嘗試'長',但爲什麼它會影響假設行/列不在'整數'範圍之外? –

+0

這並不重要,如果是的話,錯誤將會不同。這只是一件你不用擔心的事情,除非你在訪問超出整數範圍的行時故意要拋出錯誤。 – Jeeped

+0

同意這一點,這將是一個溢出eroor。但是,在宏的情況下,當前的'repoLastRow'是804,'repoLastCol'是1667,所以仍然有很多空間(對於循環的每次迭代,列依賴增加1)。此外,範圍始終是一個1維數組,從列3到1667,所以不能明白爲什麼讓Excel扼殺在這一個... –

相關問題