0

我在遍历 Excel 电子表格中的数据时遇到问题。

FOR EACH 列 A 中的值在 B 列中的所有值中搜索值。如果为真,则插入“重复”

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

我已经尝试过,但VLOOKUP一直无法解决这个问题。MATCHINDEXMATCH

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

4

1 回答 1

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
于 2012-09-25T06:40:42.333 回答