我有90,000多行数据要合并。我的代码工作正常,最多可达25k行。但是当有更多数据时,它会在32267行停止并显示对象错误。
我在同一型号上提到了一些Stackoverflow评论。但是他们的答案并没有帮助我解决问题。
我的代码
Sub macro_consolidator()
Dim spendSheet As Worksheet
Dim outputSheet As Worksheet
Dim i As Long
Dim spendLastRow As Long
Dim outputLastRow As Long
Dim spendID As String
Dim outputIDRow As Variant
Dim ctSheet As Worksheet
Dim sh As Worksheet, flg As Boolean
Application.ScreenUpdating = False
Set spendSheet = ThisWorkbook.Sheets("Sheet1")
For Each sh In Worksheets
If sh.Name Like "Output" Then flg = True: Exit For
Next
If flg = False Then
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = "Output"
Set outputSheet = ThisWorkbook.Sheets("output")
Set spendSheet = ThisWorkbook.Sheets("Sheet1")
spendLastRow = spendSheet.UsedRange.Rows.Count
outputLastRow = outputSheet.UsedRange.Rows.Count
For i = 4 To spendLastRow
spendID = CStr(spendSheet.Cells(i, 3)) + CStr(spendSheet.Cells(i, 4)) + CStr(spendSheet.Cells(i, 5)) + CStr(spendSheet.Cells(i, 6)) + CStr(spendSheet.Cells(i, 10)) + CStr(spendSheet.Cells(i, 15)) + CStr(spendSheet.Cells(i, 16))
outputIDRow = Application.Match(spendID, outputSheet.Columns(35), 0)
If Not IsError(outputIDRow) Then
outputSheet.Cells(outputIDRow, 11) = spendSheet.Cells(i, 14)
outputSheet.Cells(outputIDRow, 12) = outputSheet.Cells(outputIDRow, 12) + spendSheet.Cells(i, 13) ' Actual Spend - Addition wit previous
If spendSheet.Cells(i, 1) = "CORPORATE" Then
MsgBox (outputIDRow):
outputSheet.Cells(outputIDRow, 14) = outputSheet.Cells(outputIDRow, 14) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "CCP" Then
outputSheet.Cells(outputIDRow, 13) = outputSheet.Cells(outputIDRow, 13) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 2" Then
outputSheet.Cells(outputIDRow, 15) = outputSheet.Cells(outputIDRow, 15) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 3" Then
outputSheet.Cells(outputIDRow, 16) = outputSheet.Cells(outputIDRow, 16) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 3E" Then
outputSheet.Cells(outputIDRow, 17) = outputSheet.Cells(outputIDRow, 17) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 5" Then
outputSheet.Cells(outputIDRow, 18) = outputSheet.Cells(outputIDRow, 18) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 6" Then
outputSheet.Cells(outputIDRow, 19) = outputSheet.Cells(outputIDRow, 19) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 7" Then
outputSheet.Cells(outputIDRow, 20) = outputSheet.Cells(outputIDRow, 20) + spendSheet.Cells(i, 9)
End If
' MsgBox (outputSheet.Cells(3, 13))
Else
outputLastRow = outputLastRow + 1
outputSheet.Cells(outputLastRow, 35) = spendID
outputSheet.Cells(outputLastRow, 1) = spendSheet.Cells(i, 2)
outputSheet.Cells(outputLastRow, 2) = spendSheet.Cells(i, 5)
outputSheet.Cells(outputLastRow, 3) = spendSheet.Cells(i, 4) ' was (i,5)
outputSheet.Cells(outputLastRow, 4) = spendSheet.Cells(i, 10)
outputSheet.Cells(outputLastRow, 5) = spendSheet.Cells(i, 16)
outputSheet.Cells(outputLastRow, 6) = spendSheet.Cells(i, 15)
outputSheet.Cells(outputLastRow, 7) = spendSheet.Cells(i, 6)
'outputSheet.Cells(outputLastRow, 8) = spendSheet.Cells(i, 14)
' outputSheet.Cells(outputLastRow, 9) = spendSheet.Cells(i, 11)
outputSheet.Cells(outputLastRow, 10) = spendSheet.Cells(i, 3)
outputSheet.Cells(outputLastRow, 11) = spendSheet.Cells(i, 14) ' Unit Price
outputSheet.Cells(outputLastRow, 12) = spendSheet.Cells(i, 13) ' Actual USD
'outputSheet.Cells(outputLastRow, 13) = outputSheet.Cells(outputLastRow, 13) + spendSheet.Cells(i, 19)
' outputSheet.Cells(outputLastRow, 13) = spendSheet.Cells(i, 19)
If spendSheet.Cells(i, 1) = "CORPORATE" Then
MsgBox (outputLastRow):
outputSheet.Cells(outputLastRow, 14) = outputSheet.Cells(outputLastRow, 14) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "CCP" Then
outputSheet.Cells(outputLastRow, 13) = outputSheet.Cells(outputLastRow, 13) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 2" Then
outputSheet.Cells(outputLastRow, 15) = outputSheet.Cells(outputLastRow, 15) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 3" Then
outputSheet.Cells(outputLastRow, 16) = outputSheet.Cells(outputLastRow, 16) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 3E" Then
outputSheet.Cells(outputLastRow, 17) = outputSheet.Cells(outputLastRow, 17) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 5" Then
outputSheet.Cells(outputLastRow, 18) = outputSheet.Cells(outputLastRow, 18) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 6" Then
outputSheet.Cells(outputLastRow, 19) = outputSheet.Cells(outputLastRow, 19) + spendSheet.Cells(i, 9)
End If
If spendSheet.Cells(i, 1) = "FAB 7" Then
outputSheet.Cells(outputLastRow, 20) = outputSheet.Cells(outputLastRow, 20) + spendSheet.Cells(i, 9)
End If
End If
Next i
End Sub