查找在其他列中找不到的数字

时间:2014-11-28 20:46:32

标签: excel excel-vba excel-formula vba

我有这个excel公式,它会查找在另一列中找不到的项目,并根据结果写入是或否。我怎么能这样做但在excel中使用vba?

=IFERROR(IF(MATCH(B2,$A:$A,0),"yes",),"no")

我是vba和excel的新手,感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

以下代码使用Match函数将B列中的值与A列中的值进行比较,并在C列中写入结果(是或否):

Sub CompareColumns()

Dim aLastRow As Long
Dim bLastRow As Long
Dim i As Long
Dim result As Variant

aLastRow = ThisWorkbook.ActiveSheet.Range("A1000").End(xlUp).Row
bLastRow = ThisWorkbook.ActiveSheet.Range("B1000").End(xlUp).Row

For i = 1 To bLastRow

    result = Application.Match(ThisWorkbook.ActiveSheet.Range("B" & i).Value, ThisWorkbook.ActiveSheet.Range("A1:A" & aLastRow), 0)

    If IsError(result) Then
        ThisWorkbook.ActiveSheet.Range("C" & i).Value = "No"
    Else
        ThisWorkbook.ActiveSheet.Range("C" & i).Value = "Yes"
    End If

Next i

End Sub

答案 1 :(得分:1)

考虑:

Sub dural()
    Dim a As Range, b As Range
    Set a = Range("A:A")
    Set b = Range("B2")
    If Application.WorksheetFunction.CountIf(a, b) > 0 Then
        MsgBox "yes"
    Else
        MsgBox "no"
    End If
End Sub