VBA Macro Excel to group, find, and remove duplicates

I have no idea how to do this, so I ask about it here. So this is the CSV in my excel:

enter image description here

As you can see, we have dusplicates in CSV both at the price and at the special price. Prices are tied to size. You can see that the size is β€œthe same”, but there are spaces in them.

What I need?

VBA needs to group each sku, for example:

enter image description here

After that, he needs to find duplicate prices and select these rows and remove duplicates, and NOT by default, for example:

enter image description here

The next SKU group:

enter image description here
At least I think this is the procedure for this, if there is another way, I would like to hear it.


For me, I have no idea how I should do this. Maybe an Excel function?
Is it possible?

UPDATE 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

+4
3

, , . .

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
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
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
0

Source: https://habr.com/ru/post/1617707/


All Articles