如何突出显示表格的2列中的所有重复项? (Vba,excel)

时间:2015-11-15 23:39:40

标签: excel vba excel-vba

我正在学习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

表:Table

1 个答案:

答案 0 :(得分:0)

关于你的上一个问题/评论:

activeCell1声明为变量类型Range

Dim activeCell1 As Range

然后Set Range object赞,

Set activeCell1 = Cells(activeRow, 3)

正确设置范围var后,您可以访问Range.Value property。对所有范围类型变量重复此操作,然后您可以使用它们,如

If comparisonCell1.Value = activeCell1.Value And comparisonCell2.Value = activeCell2.Value Then