繁体   English   中英

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

[英]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.

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