clear duplicated items in specific columns and arrange below for each item based on column

Mussa

Active Member
Joined
Jul 12, 2021
Messages
264
Office Version
  1. 2019
  2. 2010
hi
I search for macro to clear repeated items in COL A and arrange again below for each item .



sheet data
ttt.xlsm
ABCDEF
1DEL NOBATCH NO TTLTT1IMPORTEXPORT
2CCD-1CC-1SS-1TRU1210
3CCD-1CC-1SS-1LTR125
4CCD-1CC-2SS-2FG55
5CCS-2CC-1SS-1TRR105
6CCS-2CC-1SS-1LTR2010
7CSD-1CS-1LL-1RRL155
8CSD-1CS-2LL-2TTY1010
9CCD-1CS-3LL-3MMW2010
10CCLCS-4LL-4NNW1010
11CCLCC-2SS-2LTR215
12CCMCC-3SS-3LTR225
13CSD-1CS-1LL-1RRL155
DATA


expected result
sheet result
ttt.xlsm
ABCDEF
1DEL NOBATCH NO TTLTT1IMPORTEXPORT
2CCD-1CC-1SS-1TRU1210
3CC-1SS-1LTR125
4CC-2SS-2FG55
5CS-3LL-3MMW2010
6CCS-2CC-1SS-1TRR105
7CC-1SS-1LTR2010
8CSD-1CS-1LL-1RRL155
9CS-2LL-2TTY1010
10CS-1LL-1RRL155
11CCLCS-4LL-4NNW1010
12CC-2SS-2LTR215
13CCMCC-3SS-3LTR225
RESULT
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
This does not put the groups in the same order as your expected results, but would this suffice?
Note that this also replaces the original data. I wasn't sure if your two mini-sheets were just to show the original and the result or whether you actually want the results on a different sheet.

Test with a copy of your workbook

VBA Code:
Sub Mussa()
  With Range("A1").CurrentRegion
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
    With .Columns(1).Offset(1).Resize(.Rows.Count - 1)
      .Value = Evaluate(Replace(Replace("if(#=%,True,#)", "#", .Address), "%", .Offset(-1).Address))
      On Error Resume Next
      .SpecialCells(xlConstants, xlLogical).ClearContents
      On Error GoTo 0
    End With
  End With
End Sub

With your original sample data, this is the result for me.

Mussa_1.xlsm
ABCDEF
1DEL NOBATCH NO TTLTT1IMPORTEXPORT
2CCD-1CC-1SS-1TRU1210
3CC-1SS-1LTR125
4CC-2SS-2FG55
5CS-3LL-3MMW2010
6CCLCS-4LL-4NNW1010
7CC-2SS-2LTR215
8CCMCC-3SS-3LTR225
9CCS-2CC-1SS-1TRR105
10CC-1SS-1LTR2010
11CSD-1CS-1LL-1RRL155
12CS-2LL-2TTY1010
13CS-1LL-1RRL155
Sheet1
 
Upvote 0
awsome !
but would this suffice?
yes
but I have two thing . may you make the result in sheet2 instead of the same sheet ?
and explain your code by writing the comments inside it,please ?
 
Upvote 0
may you make the result in sheet2 instead of the same sheet
Does sheet2 already exist in the workbook?

If so, what should happen to any existing data that is already in sheet2?
 
Upvote 0
it should replace the data . it' s like updating. if there are the same items then should updating based on sheet1 and if there is a new item then should copy to the bottom
 
Upvote 0
Alternative for result without sorting,
VBA Code:
Sub test()
    Dim s1 As Worksheet, s2 As Worksheet, _
        data(), yData(), i&, ky$, itms(), itm, _
        bl, say&, ii&
    Set s1 = Sheets("DATA")
    Set s2 = Sheets("RESULT")
    data = s1.Range("A2:F" & s1.Cells(Rows.Count, 1).End(3).Row).Value
    ReDim yData(1 To UBound(data), 1 To UBound(data, 2))
    With CreateObject("Scripting.Dictionary")
        For i = LBound(data) To UBound(data)
            ky = data(i, 1)
            If Not .exists(ky) Then
                .Item(ky) = i
            Else
                .Item(ky) = .Item(ky) & "," & i
            End If
        Next i
        itms = .items
        For Each itm In itms
            bl = Split(itm, ",")
            For i = 0 To UBound(bl)
                say = say + 1
                For ii = 2 To 6
                    yData(say, ii) = data(bl(i), ii)
                Next ii
                If i = 0 Then yData(say, 1) = data(bl(i), 1)
            Next i
        Next
    End With
    s2.Cells.ClearContents
    s1.Range("A1").Resize(, 6).Copy s2.Range("A1")
    s2.Range("A2").Resize(say, UBound(data, 2)).Value = yData

End Sub
 
Upvote 0
it should replace the data . ........ and if there is a new item then should copy to the bottom
That seems contradictory to me. It either replaces the data that is already on Sheet2 or it appends the data to the bottom of what is already on Sheet2?
 
Upvote 0
That seems contradictory to me. It either replaces the data that is already on Sheet2 or it appends the data to the bottom of what is already on Sheet2?

sorry. actually ther is no data in sheet2 . I'm talking about when run macro from the first time . it just replaces the data when run macro repeatedly
 
Upvote 0
OK, try this

VBA Code:
Sub Mussa_v2()
  With Sheets("Sheet2")
    'Clear any existing data
    .UsedRange.Clear
    'Copy the table from Sh1 to Sh2
    Sheets("Sheet1").Range("A1").CurrentRegion.Copy .Range("A1")
    With .Range("A1").CurrentRegion
      'Sort based on column A to get all the groups together
      .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
      With .Columns(1).Offset(1).Resize(.Rows.Count - 1)
        'Look at the column A cells from row 2 down and replace with True if they are the same as the cell above
        .Value = Evaluate(Replace(Replace("if(#=%,True,#)", "#", .Address), "%", .Offset(-1).Address))
        'Clear contents from all cells that contain True
        On Error Resume Next
        .SpecialCells(xlConstants, xlLogical).ClearContents
        On Error GoTo 0
      End With
    End With
  End With
End Sub
 
Upvote 0
@Peter_SSs when I add repeated items or new items in sheet1 the code doesn't work well . but when I delete the data in sheet2 and run the macro again it works well . how I can fix it this problem without I have to delete data in sheet2 when I add the data in sheet1 whether repeated items or a new.
sheet1
ttt.xlsm
ABCDEF
1DEL NOBATCH NOTTLTT1IMPORTEXPORT
2CCD-1CC-1SS-1TRU1210
3CCD-1CC-1SS-1LTR125
4CCD-1CC-2SS-2FG55
5CCS-2CC-1SS-1TRR105
6CCS-2CC-1SS-1LTR2010
7CSD-1CS-1LL-1RRL155
8CSD-1CS-2LL-2TTY1010
9CCD-1CS-3LL-3MMW2010
10CCLCS-4LL-4NNW1010
11CCLCC-2SS-2LTR215
12CCMCC-3SS-3LTR225
13CSD-1CS-1LL-1RRL155
14CSD-2CS-2LL-2RRL155
15CSD-3CS-3LL-3RRL155
16CSD-4CS-4LL-4RRL155
17CSD-5CS-5LL-5RRL155
18CSD-1CS-1LL-1RRL155
19CSD-2CS-2LL-2RRL155
20CSD-3CS-3LL-3RRL155
21CSD-4CS-4LL-4RRL155
22CSD-5CS-5LL-5RRL155
sheet1


this is what I got
ttt.xlsm
ABCDEF
1DEL NOBATCH NOTTLTT1IMPORTEXPORT
2CCD-1CC-1SS-1TRU1210
3CC-1SS-1LTR125
4CC-2SS-2FG55
5CCS-2CS-3LL-3MMW2010
6CS-4LL-4NNW1010
7CSD-1CC-2SS-2LTR215
8CC-3SS-3LTR225
9CCD-1CC-1SS-1TRR105
10CCLCC-1SS-1LTR2010
11CS-1LL-1RRL155
12CCMCS-2LL-2TTY1010
13CSD-1CS-1LL-1RRL155
14CSD-2CS-1LL-1RRL155
15CSD-3CS-2LL-2RRL155
16CSD-4CS-2LL-2RRL155
17CSD-5CS-3LL-3RRL155
18CSD-1CS-3LL-3RRL155
19CSD-2CS-4LL-4RRL155
20CSD-3CS-4LL-4RRL155
21CSD-4CS-5LL-5RRL155
22CSD-5CS-5LL-5RRL155
sheet2
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,323
Members
453,032
Latest member
Pauh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top