【发布时间】:2015-11-10 13:55:42
【问题描述】:
我正在尝试找到一种更有效的方法来突出显示不同工作表上两个范围之间的重复单元格。下面的代码非常慢:
Sub HighlightDuplicates()
Application.DisplayAlerts = False
lrU = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lrPT = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Dim rng1, rng2, cell1, cell2 As Range
Set rng1 = Worksheets("Sheet1").Range("DL4:DL" & lrU)
Set rng2 = Worksheets("Sheet2").Range("E3:M" & lrPT)
For Each cell1 In rng1
For Each cell2 In rng2
If cell1.Value = cell2.Value Then
cell1.Font.Bold = True
cell1.Font.ColorIndex = 2
cell1.Interior.ColorIndex = 3
cell1.Interior.Pattern = xlSolid
cell2.Font.Bold = True
cell2.Font.ColorIndex = 2
cell2.Interior.ColorIndex = 3
cell2.Interior.Pattern = xlSolid
End If
Next cell2
Next cell1
Application.DisplayAlerts = True
End Sub
对更有效的方法有什么建议吗?
感谢您的帮助。
问候,
【问题讨论】:
-
你试过条件格式吗?
-
尝试在底部添加
Application.Calculation=xlCalculationManual和Application.ScreenUpdating=false at the top and 1Application.Calculation=xlCalculationAutomatic和Application.ScreenUpdating=true。 -
如果您想要高效,您可能需要避免使用 VBA,而只使用直接公式(或凯尔提到的格式)。我通常在我专用的一张纸上的列中使用一个公式(即
MATCH)... -
还可以考虑使用
Find方法。您可以只循环一个范围并尝试使用Find方法在另一个范围中查找每个值。见here。 -
对于@Kyle 的评论,条件格式不是一个选项吗?它可能会快一点,并且不需要宏/VB。
标签: excel vba duplicates highlight