我正在excel上编写一个VBA代码,使用循环遍历10000+ lines。
下面是表的一个示例

这是我写的代码:
Sub Find_Matches()
Dim wb As Workbook
Dim xrow As Long
Set wb = ActiveWorkbook
wb.Worksheets("Data").Activate
tCnt = Sheets("Data").UsedRange.Rows.Count
Dim e, f, a, j, h As Range
xrow = 2
Application.ScreenUpdating = False
Application.Calculation = xlManual
For xrow = 2 To tCnt Step 1
Set e = Range("E" & xrow)
Set f = e.Offset(0, 1)
Set a = e.Offset(0, -4)
Set j = e.Offset(0, 5)
Set h = e.Offset(0, 3)
For Each Cell In Range("E2:E" & tCnt)
If Cell.Value = e.Value Then
If Cell.Offset(0, 1).Value = f.Value Then
If Cell.Offset(0, -4).Value = a.Value Then
If Cell.Offset(0, 5).Value = j.Value Then
If Cell.Offset(0, 3).Value = h.Value Then
If (e.Offset(0, 7).Value) + (Cell.Offset(0, 7).Value) = 0 Then
Cell.EntireRow.Interior.Color = vbYellow
e.EntireRow.Interior.Color = vbYellow
End If
End If
End If
End If
End If
End If
Next
Next
End Sub正如您可以想象的那样,这需要花费大量的时间来遍历10000+行,我想找到一个更快的解决方案。我认为一定有一种方法可以避免过度循环
以下是条件:
对于每一行,如果文件中的另一行具有完全相同的内容:
然后,如果金额之和。( L)这两条匹配线为0,然后将两行都涂成黄色。
请注意,额外的列是存在的,没有被比较(例如- col )。( B)但对文件仍然很重要,不能删除,以简化程序。
运行前面的代码,在我的示例中,突出显示第2行和第5行:

发布于 2018-06-13 09:51:53
如果您的数据只有L列,那么使用下面的代码,我发现运行它所花费的时间更短.
Sub Duplicates()
Application.ScreenUpdating = False
Dim i As Long, lrow As Long
lrow = Cells(Rows.Count, 1).End(xlUp).Row
Range("O2") = "=A2&E2&F2&J2&L2"
Range("P2") = "=COUNTIF(O:O,O2)"
Range("O2:P" & lrow).FillDown
Range("O2:O" & lrow).Copy
Range("O2:O" & lrow).PasteSpecial xlPasteValues
Application.CutCopyMode = False
For i = 1 To lrow
If Cells(i, 16) = 2 Then
Cells(i, 16).EntireRow.Interior.Color = vbYellow
End If
Next
Application.ScreenUpdating = True
Range("O:P").Delete
Range("A1").Select
MsgBox "Done"
End Subhttps://stackoverflow.com/questions/50830464
复制相似问题