每次運行此代碼時,它都會崩潰,我盡我所能,但我不知道哪個部分崩潰,它不會告訴我爲什麼。我需要它來查看每個單元格,直到它們各自的數量並放入當前表單中。Excel VBA上的代碼崩潰
有沒有任何建議或看到任何可能的幫助?
Sub bringbookstogether()
Dim currentsheet As Worksheet
Set currentsheet = Application.ActiveSheet
'assigns the number to start with
Dim a, b, c, d As Integer
a = 4
b = 6
c = 3
d = 1
Dim wsheet As Worksheet
Set wsheet = Application.ActiveWorkbook.Sheets(c)
Dim wbook As Workbook
'assigns workbook numbers
If (d = 1) Then
Set wbook = Workbooks.Open("C:\Users\mminchuk\Documents\Updated MaintPrep Sheets\MaintPrep Sheet 1st.xlsm", UpdateLinks:=xlUpdateLinksAlways)
Else
If (d = 2) Then
Set wbook = Workbooks.Open("C:\Users\mminchuk\Documents\Updated MaintPrep Sheets\MaintPrep Sheet 2nd.xlsm", UpdateLinks:=xlUpdateLinksAlways)
Else
If (d = 3) Then
Set wbook = Workbooks.Open("C:\Users\mminchuk\Documents\Updated MaintPrep Sheets\MaintPrep Sheet 3rd.xlsm", UpdateLinks:=xlUpdateLinksAlways)
End If
End If
End If
Application.ScreenUpdating = False
'End if it's done with all the workbooks
Do Until (d = 4)
'Looks for the sheet that has the same name
Do Until (c = 53)
If (wsheet.Name = currentsheet.Name) Then
'Ends in row 99
Do Until (b = 99)
'Ends in Column 52
Do Until (a = 52)
currentsheet.Cells(b, a) = currentsheet.Cells(b, a) + Workbooks(d).Sheets(c).Cells(b, a)
a = a + 1
Loop
b = b + 1
Loop
End If
Loop
d = d + 1
Loop
Application.ScreenUpdating = True
End Sub
有了許多嵌套循環,請縮進代碼正確,所以我們可以做的更好的感覺。很難解釋它是全部左對齊的方式。 – TomServo
我想知道爲什麼,當你每次訪問'Cells'對象時,你的**只有**〜1,091,376個循環在你的4個'Do Until'循環中。 –
我假設它是崩潰它的週期。我如何解決它? – MaxAttack102