Comibed Prouct and delete duplicate

farhan11941234

New Member
Joined
Dec 14, 2019
Messages
29
Office Version
  1. 365
Platform
  1. Windows
In Id Column there are duplicate entries but product column is different. i want to combine product line A or A+B or A+B+C in next column and delete duplicates entries in column A.
please see snapshot of spreadsheet.
 

Attachments

  • Sample.png
    Sample.png
    8.7 KB · Views: 16
data in columns C:F is required
I know that

BUT

Using your first 2 rows as illustration
100001 A John 123 345 (no rate in Column E)
100001 B John 123 2 345 (rate 2 in column E)


Output required is
100001 A+B John 123 2 345 (rate from B ?? why ??)


how does VBA know it is not this ?
100001 A+B John 123 345 (no rate)
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Rate has value only if Product Has B Value, just a thought if we combined the product first then remove the rows where duplicates Id has product A and C and Unique remains the same.
 
Upvote 0
Try this
VBA Code:
Sub farhan11941234()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, Cel As Range, Rng As Range, Prod As Range, F As Long, Addr As String
    Dim MainStr As String, ProdStr As String
  
 'copy to new sheet and remove duplicates
    ActiveSheet.Copy Before:=Sheets(1)
    Set ws = Sheets(1)
    ws.Cells(1, 7) = "Combine Products"
'sort data by ID and then by Product
    With ws.Sort
        .SortFields.Add Key:=Range("A1"), Order:=xlAscending
        .SortFields.Add Key:=Range("B1"), Order:=xlAscending
        .SetRange Range("A:G")
        .Header = xlYes
        .Apply
    End With
'concatenate strings
    Set Rng = ws.Range("A2", ws.Range("A" & ws.Rows.Count).End(xlUp))
    For Each Cel In Rng
        ProdStr = Cel.Offset(, 1)
        If WorksheetFunction.CountIf(ws.Range("A2", Cel), Cel) = 1 Then
            MainStr = ProdStr
            Set Prod = Cel.Offset(, 6)
        Else
            MainStr = MainStr & "+" & ProdStr
            'reset where to write values to
            If Cel.Offset(, 1) = "B" Then Set Prod = Cel.Offset(, 6)
        End If
        If Cel.Offset(1) <> Cel Then Prod = MainStr
    Next Cel
'remove unwnated rows
    Set Prod = ws.Cells(ws.Rows.Count, 7)
    For Each Cel In Rng.Offset(, 6)
        If Cel = "" Then Set Prod = Union(Prod, Cel)
    Next Cel
    Prod.EntireRow.Delete
'place returned combined values in column B
    Rng.Offset(, 1).Value = Rng.Offset(, 6).Value
    ws.Range("G1").EntireColumn.Delete
End Sub

This is how the cell for combined values is reset If there any entries for product B for an ID
Code:
            If Cel.Offset(, 1) = "B" Then Set Prod = Cel.Offset(, 6)

Result based on data supplied

Book1
ABCDEFG
1IDProductNameAddrssRateCell No
2100001A+BJohn1232345
3100002A+B+CBillxyz2321
4100003A+B+CWillAsd4301
5100004A+BHarryGhq6300
6100005ASupermanjkl390
7100006ABatmanmno287
8100007AJokerqwe210
9100008A+B+CAlenPoiuy8654
10
Sheet4 (14)
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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