每次运行此代码时,它都会崩溃,我尽我所能,但我不知道哪个部分崩溃,它不会告诉我为什么。我需要它来查看每个单元格,直到它们各自的数量并放入当前表单中。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