在2个不同的行中查找匹配-如果其他失败

时间:2018-09-17 10:59:09

标签: excel vba excel-vba

早上好

我在多个文档中发现以下代码:

Sub Find_match()

    Dim Range1 As Range
    Dim Range2 As Range

    Set Range1 = Application.InputBox("Select the range on the first sheet to
    Compare", Type:=8)

    Set Range2 = Application.InputBox("Select the range on the second sheet to
    Compare", Type:=8)

    Dim range_row1 As Integer
    Dim range_row2 As Integer
    Dim loop_row1 As Integer
    Dim loop_row2 As Integer
    Dim find_value As String

    range_row1 = Range1.Rows.Count
    range_row2 = Range2.Rows.Count

    For loop_row1 = 1 To range_row1
        find_value = Range1.Cells(loop_row1, 1)

        For loop_row2 = 1 To range_row2
            If Range2.Cells(loop_row2, 1) = find_value Then
                If Range1.Cells(loop_row1, 2) = Range2.Cells(loop_row2, 2) Then

                    Range1.Rows(loop_row1).Interior.Color = RGB(153, 204, 0)
                    Range2.Rows(loop_row2).Interior.Color = RGB(153, 204, 0)

                End If
                ' Else
                '   Range1.Rows(loop_row1).Interior.Color = RGB(255, 153, 0)
            End If

        Next

    Next

End Sub

但是,当尝试放置else子句以允许其余单元格以不同颜色“绘制”时,实际上会将最后一个颜色叠加在一些重复的先前值上。

0 个答案:

没有答案