[英]How do i highlight all duplicates in 2 columns of a table? (Vba, excel)
我正在學習vba。
我正在嘗試編寫將第3列中的每個單元格與同一列中的每個進行中的單元格進行比較的代碼,將第5列中的每個單元格與同一列中的每個進行中的單元格進行比較; 並突出顯示任何比較單元格(如果有的話),並且同一行另一列中的單元格都與各自列中要比較的單元格匹配。
碼:
Sub comparisonDuplicateHighlight()
Set rng = Rows
Dim activeRow As Integer
activeRow = 2
Dim activeCell1 As Cells
activeCell1 = Cells(activeRow, 3)
Dim activeCell2 As Cells
acriveCell2 = Cells(activeRow, 5)
Dim comparisonRow As Integer
comparisonRow = activeRow + 1
Dim comparisonCell1 As Cells
comparisonCell1 = Cells(comparisonRow, 3)
Dim comparisonCell2 As Cells
comparisonCell2 = Cells(comparisonRow, 5)
For rng = 2 To 25
If comparisonCell1.Value = activeCell1.Value And comparisonCell2.Value = activeCell2.Value Then
comparisonCell1.Interior.ColorIndex = 6
comparisonCell2.Interior.ColorIndex = 6
Else
comparisonRow1 = comparisonRow1 + 1
comparisonRow2 = comparisonRow2 + 1
End If
End Sub
表格: 表格
關於您的最后一個問題/評論:
將activeCell1
聲明為變量類型Range
。
Dim activeCell1 As Range
然后Set
Range對象 Set
為
Set activeCell1 = Cells(activeRow, 3)
正確設置范圍var后,您可以訪問Range.Value屬性 。 對所有范圍類型var重復此操作,然后可以像這樣使用它們,
If comparisonCell1.Value = activeCell1.Value And comparisonCell2.Value = activeCell2.Value Then
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.