一個宏(不是我的,但我「繼承」它)運行多個循環。該代碼有幾千行,所以我只提供片段中循環的部分。隨機'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的問題信息。
有關問題性質的任何猜測?
您現在可能會注意到,編碼部分不顯示粗體。哪一行產生錯誤? – Jeeped
編輯器用**標記它。我的意思是:Range(Cells(repoLastRow + 1,3),Cells(repoLastRow + 1,repoLastCol))。Value = _ 範圍(Cells(repoLastRow + 1,3),Cells(repoLastRow + 1,repoLastCol ))。值' –
相關主題:您是否正在返回一個字符串或一個包含該'VLOOKUP(「&column_letter&」1&「&column_letter&」2,...'的數字?如果您期待一個字符串並且不想要一個零時,當你打一個空白有一個更好的方法,但不建議如果你期望一個數字返回。 – Jeeped