简体   繁体   English

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

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

i'm learning vba. 我正在学习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; 我正在尝试编写将第3列中的每个单元格与同一列中的每个进行中的单元格进行比较的代码,将第5列中的每个单元格与同一列中的每个进行中的单元格进行比较; 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 . activeCell1声明为变量类型Range

Dim activeCell1 As Range

Then Set the Range object like, 然后Set Range对象 Set

Set activeCell1 = Cells(activeRow, 3)

With the range var properly set you can access the Range.Value property . 正确设置范围var后,您可以访问Range.Value属性 Repeat this for all of the range type vars then you can use them like, 对所有范围类型var重复此操作,然后可以像这样使用它们,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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