VBA宏Excel用于分组,查找和删除重复项

时间:2015-11-27 08:43:19

标签: excel vba excel-vba

我不知道怎么做,所以我在这里问 所以这是我在excel中的CSV:

enter image description here

正如您所看到的,我们在价格和特价方面都在CSV中进行了重复 价格必然会有规模。您可以看到大小“相同”但它们中有空格。

我需要什么?

VBA需要对每个sku进行分组,例如:

enter image description here

在此之后,它需要找到重复的价格并选择那些行并删除重复项而不是默认值,例如:

enter image description here

下一个SKU小组:

enter image description here
至少,我认为这是这样做的过程,如果有其他方式,我想听听。


对我来说,我不知道该怎么做。也许Excel功能?
这甚至可能吗?

更新1

所以我尝试了R3uK的解决方案和Marco Getrost的解决方案,R3uK对我来说做得最好。

我已将VBA改为喜欢我的大CSV 这是:

Sub test_Sj03rs()
With ActiveSheet
    'In column D
    With .Range("Y:Y")
        'Change all double spaces to single ones (being extra careful)
        .Replace What:="  ", _
                Replacement:=" ", _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                MatchCase:=False, _
                SearchFormat:=False, _
                ReplaceFormat:=False
        'Change all slashes+spaces to single slash
        .Replace What:="/ ", _
                Replacement:="/", _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                MatchCase:=False, _
                SearchFormat:=False, _
                ReplaceFormat:=False
        'Change all spaces+slashes to single slash
        .Replace What:=" /", _
                Replacement:="/", _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                MatchCase:=False, _
                SearchFormat:=False, _
                ReplaceFormat:=False
    End With
    With .Range("A:AA")
        'To get rid of formulas if there is
        .Value = .Value
        'Remove duplicates considering all columns
        .RemoveDuplicates _
            Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27), _
            Header:=xlYes
    End With
End With
End Sub

这是应该用于的文件。请记住,此文件大约有13.000行。

enter image description here

更新2

对于想要测试的人来说,这是CSV。

CSV

3 个答案:

答案 0 :(得分:0)

我假设您实际使用了示例中描述的设置。否则,您可能希望对代码进行一些更改。

Sub test()

Dim rN&

With ActiveSheet

    .Columns("D").Replace What:=" ", Replacement:="", LookAt:=xlPart, SearchOrder:= _
        xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

    rN = 1

    Do While .Cells(rN, 1).Value <> ""

        Do While (.Cells(rN, 1).Value = .Cells(rN + 1, 1).Value And .Cells(rN, 4).Value = .Cells(rN + 1, 4).Value _
        And .Cells(rN, 2).Value = .Cells(rN + 1, 2).Value And .Cells(rN, 3).Value = .Cells(rN + 1, 3).Value)

            .Cells(rN + 1, 1).EntireRow.Delete

        Loop

        rN = rN + 1

    Loop

End With
End Sub

答案 1 :(得分:0)

这应该可以解决问题:

Sub test_Sj03rs()
Dim r as Range
With ActiveSheet
    'In column U (QTY)
    For Each r in .Range("Y:Y").Cells
        r.Value = r.Value * 1
    Next r
    'In column Y
    With .Range("Y:Y")
        'Change all double spaces to single ones (being extra careful)
        .Replace What:="  ", _
                Replacement:=" ", _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                MatchCase:=False, _
                SearchFormat:=False, _
                ReplaceFormat:=False
        'Change all slashes+spaces to single slash
        .Replace What:="/ ", _
                Replacement:="/", _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                MatchCase:=False, _
                SearchFormat:=False, _
                ReplaceFormat:=False
        'Change all spaces+slashes to single slash
        .Replace What:=" /", _
                Replacement:="/", _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                MatchCase:=False, _
                SearchFormat:=False, _
                ReplaceFormat:=False
    End With
    With .Range("A:AA")
        'To get rid of formulas if there is
        .Value = .Value
        'Remove duplicates considering all columns
        .RemoveDuplicates _
            Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27), _
            Header:=xlYes
    End With
End With
End Sub

答案 2 :(得分:0)

可怕我知道,但如果你没有那么多的数据,这应该有效。

Sub remove()

Dim lRowNo As Long Dim lCheckRow As Long Dim sString As String Dim sCheckString As String

    For lRowNo = 2 To ActiveSheet.UsedRange.Rows.Count

        sString = Replace(Cells(lRowNo, 4), " ", "")

        For lCheckRow = 2 To lRowNo - 1

            sCheckString = Replace(Cells(lCheckRow, 4), " ", "")

            If sString = sCheckString Then

                Rows(lRowNo).EntireRow.Delete
                    lRowNo = lRowNo -1
                    exit for

            End If

        Next lCheckRow

    Next lRowNo

End Sub