用于比较列的VBA代码

时间:2018-03-08 08:48:30

标签: excel vba excel-vba

我正在寻找比较两列的VBA代码,如果匹配则在第三列显示YES,否则它将显示NO。

我试过了。

Sub Find_Matches()
    Dim CompareRange As Variant, x As Variant, y As Variant
    ' Set CompareRange equal to the range to which you will
    ' compare the selection.
    Set CompareRange = Range("C1:C5")
    ' NOTE: If the compare range is located on another workbook
    ' or worksheet, use the following syntax.
    ' Set CompareRange = Workbooks("Book2"). _
    '   Worksheets("Sheet2").Range("C1:C5")
    '
    ' Loop through each cell in the selection and compare it to
    ' each cell in CompareRange.
    For Each x In Selection
        For Each y In CompareRange
            If x = y Then x.Offset(0, 1) = x
        Next y
    Next x
End Sub

2 个答案:

答案 0 :(得分:0)

试试这个。

subscribeOn

答案 1 :(得分:0)

你可以用这个

Sub Find_Matches()
    Dim CompareRange As Variant, x As Variant

    Set CompareRange = Range("C1:C5")
    For Each x In Selection
        x.Offset(0, 1) = IIf(IsError(Application.Match(x, CompareRange, 0)), "NO", "YES")
    Next x
End Sub

当然,这将在所选单元格的右侧写一列