简体   繁体   中英

How do i highlight all duplicates in 2 columns of a table? (Vba, excel)

i'm learning vba.

I'm trying to write code which will compare each cell in col 3 to each proceeding cell in the same column, compare each cell in col 5 to each proceeding cell in the same column; and highlight any comparison cell if it, and the cell in the other column of the same row both match the cells they are being compared to in their respective columns.

Code:

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

Sheet: Table

Regarding your last question/comment:

Declare activeCell1 as a variable type Range .

Dim activeCell1 As Range

Then Set the Range object like,

Set activeCell1 = Cells(activeRow, 3)

With the range var properly set you can access the Range.Value property . Repeat this for all of the range type vars then you can use them like,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM