在A列中循环列B的foreach值以查找重复项

时间:2012-09-25 06:07:22

标签: excel vba excel-vba excel-formula

我无法在Excel电子表格中循环浏览数据。

对于A列中的每个值,搜索B列中所有值中的值。 IF True 插入“重复”

到目前为止,我尝试了多种变体:=IF(A2=$B$2:$B$14,"duplicate",)

我已尝试VLOOKUPMATCH以及INDEX MATCH,但未能解决这个问题。

我相信vba脚本会更有效率,但似乎无法弄明白,你能帮忙吗?

1 个答案:

答案 0 :(得分:8)

要使用公式来完成,因为您的帖子建议您尝试使用

=IF(ISNA(MATCH(A2,B:B,0)),"","duplicate")

对于VBA解决方案,请尝试此

Sub CheckForDups()
    Dim rSource As Range
    Dim rCompare As Range
    Dim rResult As Range
    Dim vSource As Variant
    Dim vComapre As Variant
    Dim vResult As Variant
    Dim sh As Worksheet
    Dim i As Long

    Set sh = ActiveSheet
    Set rSource = Application.Intersect(sh.UsedRange, sh.Columns(1))
    Set rCompare = Application.Intersect(sh.UsedRange, sh.Columns(2))
    Set rResult = Application.Intersect(sh.UsedRange, sh.Columns(3))
    vSource = rSource
    vComapre = rCompare
    rResult.Clear
    vResult = rResult
    For i = 2 To UBound(vSource, 1)
        If Not IsError(Application.Match(vSource(i, 1), rCompare, 0)) Then
            vResult(i, 1) = "duplicate"
        End If
    Next
    rResult = vResult
End Sub