如何在Excel中查找重复项并在第二列中列出具有相似值

时间:2015-05-07 04:35:50

标签: excel excel-vba vba

我正在实施一个新的库存计划,我正在尝试在完整的Excel导出中找到重复的SKU。所有SKU都有类似的(类似)值。例如,一个SKU是ABC123,接下来的几个SKU是SKU:QWABC123WBI,它们是完全相同的SKU。

我需要将它们并排放在所有类似值的旁边,或者如何在一列上的Excel 2007中找到“like”值,并将其显示在另一列上。

例如,这些是值/单元格列A
的productid / SKU

ABC123, 
QWABC123WBI,
ASCABC123FWF,
QWEEVSDVB444,
FT43TGGGYHHRTH,
GHTRGHY5Y,

因此,列BC应显示:

**DUPLICATE 1   DUPLICATE 2**:
QWABC123WBI,    ASCABC123FWF

1 个答案:

答案 0 :(得分:0)

你需要一个像这样的VBA宏:

Const storeColumn As Long = 1    'This defines to column number of your data

Sub findAllLikes()
    Dim ws As Worksheet
    Dim r As Range

    Set ws = ActiveSheet
    Set r = ws.Range(ws.Cells(1, storeColumn), ws.Cells(ws.UsedRange.Rows.Count, storeColumn))

    Dim cell As Range, dupCell As Range
    Dim dupColumn As Long, str As String

    For Each cell In r
        If Trim(cell.Value) <> "" Then
            dupColumn = storeColumn + 1
            For Each dupCell In r
                If cell.Row <> dupCell.Row Then
                    str = Trim(cell.Value)
                    If (Right(str, 1) = ",") Then str = Left(str, Len(str) - 1)
                    If (dupCell.Value Like "*" & str & "*") Then
                        ws.Cells(cell.Row, dupColumn).Value = dupCell.Value
                        dupColumn = dupColumn + 1
                    End If
                End If
            Next
        End If
    Next
End Sub

您可以按 F5 运行宏。