我有一些代碼將兩行合併爲一個基於匹配引用的代碼。最初有10列,一旦行被組合,它將變成20列。根據匹配ref將兩行合併爲一個非常慢
該代碼有效,但速度很慢。它幾乎就像循環表格中的每一行,而不僅僅是基於「LastRow」變量。這是問題還是其他問題? 如果我關閉更新,它仍然很慢。如果我將它們留在屏幕上,只會一直閃爍,直到在任務管理器中關閉它爲止。
Sub CombineRows()
'define variables
Dim RowNum As Long, LastRow As Long
Application.ScreenUpdating = False
'start below titles and make full selection of data
RowNum = 2
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A2", Cells(LastRow, 10)).Select
'For loop for all rows in selection with cells
For Each Row In Selection
With Cells
'if order number matches
If Cells(RowNum, 4) = Cells(RowNum + 1, 4) Then
'move attribute 2 up next to attribute 1 and delete empty line
Cells(RowNum + 1, 1).Copy Destination:=Cells(RowNum, 11)
Cells(RowNum + 1, 2).Copy Destination:=Cells(RowNum, 12)
Cells(RowNum + 1, 3).Copy Destination:=Cells(RowNum, 13)
Cells(RowNum + 1, 4).Copy Destination:=Cells(RowNum, 14)
Cells(RowNum + 1, 5).Copy Destination:=Cells(RowNum, 15)
Cells(RowNum + 1, 6).Copy Destination:=Cells(RowNum, 16)
Cells(RowNum + 1, 7).Copy Destination:=Cells(RowNum, 17)
Cells(RowNum + 1, 8).Copy Destination:=Cells(RowNum, 18)
Cells(RowNum + 1, 9).Copy Destination:=Cells(RowNum, 19)
Cells(RowNum + 1, 10).Copy Destination:=Cells(RowNum, 20)
Rows(RowNum + 1).EntireRow.Delete
End If
End With
'increase rownum for next test
RowNum = RowNum + 1
Next Row
'turn on screen updating
Application.ScreenUpdating = True
End Sub
很好的覆蓋和修改。 – brettdj