concatenate the values and summing with comma

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
400
Office Version
  1. 2016
Platform
  1. Windows
hi
I have theses data it should merge duplicated value in COL B,C,D and show all of values even duplicated so the result should show from from I1 : M
in COL J ignore duplicated COL K there is no duplicated based on COL B,C always are different so the duplicated in COL B,C when merge in COL I, J and in COL L should bring all the values and COL M should summing the values
INVE1 (2) (3).xlsm
ABCDE
1itemGOODSTYPEPRQTY
21FRBANANASO355
32VEGTOMATOEG417
43FO2TUNE180GTH115
54FO1TUNE160GSP55
66FO3CHEESE CHEEDERNE175
77FRAPPLETU125
88VEGPOTATOGR180
99FO5BISCUITS SWSW295
1010FO1TUNE160GIN151
1111FO2TUNE180GTW258
1212FO3CHEESE MOZZIRELAIT168
1313FO5BISCUITS SWSS1124
1414FRAPPLEIT463
1515VEGONIONPO1580
1616VEGONIONLOC1790
1717FO6BISCUITS SWBR990
1818FO7BISCUITS SWIT550
1919FO7BISCUITS SWIL560
2020FRGRAPESLOC5
2114FRBANANAEU22
RESULT1


expected result
INVE1 (2) (3).xlsm
IJKLM
1GOODSTYPEPRaaTOTAL
2FRBANANA,APPLE, GRAPESSO,TU,IT, ,LOC,EU355,125,463, 5,22970
RESULT1
 
With Power Pivot, create a measure for each of the values.

Book1
GHIJK
1GOODSTypesPRsQTYsTotal
2FO1TUNE160G, TUNE160GSP, IN55, 151206
3FO2TUNE180G, TUNE180GTH, TW115, 258373
4FO3CHEESE CHEEDER, CHEESE MOZZIRELANE, IT175, 168343
5FO5BISCUITS SW, BISCUITS SWSW, SS295, 11241419
6FO6BISCUITS SWBR990990
7FO7BISCUITS SW, BISCUITS SWIT, IL550, 5601110
8FRBANANA, APPLE, APPLE, GRAPES, BANANASO, TU, IT, LOC, EU355, 125, 463, 5, 22970
9VEGTOMATO, POTATO, ONION, ONIONEG, GR, PO, LOC417, 180, 1580, 17903967
Sheet1


Examples of the Pivot set up and how to create each measure is shown in the pictures.
 

Attachments

  • CaptureTotal.JPG
    CaptureTotal.JPG
    38.5 KB · Views: 7
  • CaptureMeasure.JPG
    CaptureMeasure.JPG
    42.3 KB · Views: 7
  • CapturePivot.JPG
    CapturePivot.JPG
    38.7 KB · Views: 7
Upvote 0
thanks for your suggestion . I can't deal with PQ or pivot , so I prefer by macro
 
Upvote 0
See if this macro does what you want...
VBA Code:
Sub Goods()
  Dim X As Long, QTY As Double, Total As Double
  Dim PR As String, Uniques As String
  Dim Ky As Variant, Data As Variant, Rick
  Dim WS As Worksheet
  Set WS = Sheets("Sheet2")
  Data = WS.Range("A1").CurrentRegion.Value
  With CreateObject("Scripting.Dictionary")
    For X = 2 To UBound(Data)
      QTY = Val(.Item(Data(X, 2)))
      PR = Split(.Item(Data(X, 2)) & "+", "+")(1)
      QTY = QTY + Data(X, 5)
      PR = PR & ", " & Data(X, 4)
      .Item(Data(X, 2)) = QTY & "+" & PR
    Next
    Ky = .Keys
    WS.Range("G1:I1") = Array("Goods", "PR", "QTY")
    For X = 0 To .Count - 1
      WS.Cells(X + 2, "G").Value = Ky(X)
      WS.Cells(X + 2, "H").Value = Mid(Split(.Item(Ky(X)), "+")(1), 3)
      WS.Cells(X + 2, "I").Value = Val(.Item(Ky(X)))
    Next
  End With
End Sub
 
Upvote 0
expected result
GOODS​
TYPE​
PR​
aa​
TOTAL​
FR​
BANANA,APPLE, GRAPES​
SO,TU,IT, ,LOC,EU​
355,125,463, 5,22​
970

Another

If this is the expected output, maybe the macro below (assumes data in Sheet1; output in columns I:M)
VBA Code:
Sub aTest()
    Dim dic As Object, vData As Variant
    Dim i As Long, vResult As Variant, vKey As Variant
    Dim arr(0 To 3) As Variant, lRow As Long
    
    Set dic = CreateObject("Scripting.Dictionary")
    dic.CompareMode = vbTextCompare
    
    With Sheets("Sheet1")
        vData = .Range("B2:E" & Cells(Rows.Count, "B").End(xlUp).Row)
        For i = 1 To UBound(vData, 1)
            If Not dic.exists(vData(i, 1)) Then
                dic(vData(i, 1)) = Array(vData(i, 2), vData(i, 3), vData(i, 4), vData(i, 4))
            Else
                arr(0) = dic(vData(i, 1))(0)
                If InStr(1, arr(0), vData(i, 2), vbTextCompare) = 0 Then _
                    arr(0) = dic(vData(i, 1))(0) & "," & vData(i, 2)
                arr(1) = dic(vData(i, 1))(1) & "," & vData(i, 3)
                arr(2) = dic(vData(i, 1))(2) & "," & vData(i, 4)
                arr(3) = dic(vData(i, 1))(3) + vData(i, 4)
                dic(vData(i, 1)) = arr
            End If
        Next i
        .Range("I1:M1") = Array("GOODS", "TYPE", "PR", "aa", "TOTAL")
        vResult = .Range("I2").Resize(dic.Count, 5)
        .Range("L2").Resize(dic.Count).NumberFormat = "@"
        For Each vKey In dic.Keys
            lRow = lRow + 1
            vResult(lRow, 1) = vKey
            vResult(lRow, 2) = dic(vKey)(0)
            vResult(lRow, 3) = dic(vKey)(1)
            vResult(lRow, 4) = dic(vKey)(2)
            vResult(lRow, 5) = dic(vKey)(3)
        Next vKey
        .Range("I2").Resize(dic.Count, 5) = vResult
    End With
End Sub

After macro
30062021 Testes.xlsm
IJKLM
1GOODSTYPEPRaaTOTAL
2FRBANANA,APPLE,GRAPESSO,TU,IT,LOC,EU355,125,463,5,22970
3VEGTOMATO,POTATO,ONIONEG,GR,PO,LOC417,180,1580,17903967
4FO2TUNE180GTH,TW115,258373
5FO1TUNE160GSP,IN55,151206
6FO3CHEESE CHEEDER,CHEESE MOZZIRELANE,IT175,168343
7FO5BISCUITS SWSW,SS295,11241419
8FO6BISCUITS SWBR990990
9FO7BISCUITS SWIT,IL550,5601110
Sheet1


Hope this helps

M.
 
Upvote 0
can you add the borders and formatting form col I to M as my source data

Try

VBA Code:
Sub aTest()
    Dim dic As Object, vData As Variant
    Dim i As Long, vResult As Variant, vKey As Variant
    Dim arr(0 To 3) As Variant, lRow As Long
    
    Set dic = CreateObject("Scripting.Dictionary")
    dic.CompareMode = vbTextCompare
    
    With Sheets("Sheet1")
        vData = .Range("B2:E" & Cells(Rows.Count, "B").End(xlUp).Row)
        For i = 1 To UBound(vData, 1)
            If Not dic.exists(vData(i, 1)) Then
                dic(vData(i, 1)) = Array(vData(i, 2), vData(i, 3), vData(i, 4), vData(i, 4))
            Else
                arr(0) = dic(vData(i, 1))(0)
                If InStr(1, arr(0), vData(i, 2), vbTextCompare) = 0 Then _
                    arr(0) = dic(vData(i, 1))(0) & "," & vData(i, 2)
                arr(1) = dic(vData(i, 1))(1) & "," & vData(i, 3)
                arr(2) = dic(vData(i, 1))(2) & "," & vData(i, 4)
                arr(3) = dic(vData(i, 1))(3) + vData(i, 4)
                dic(vData(i, 1)) = arr
            End If
        Next i
        'Headers
        With .Range("I1:M1")
            .Value = Array("GOODS", "TYPE", "PR", "aa", "TOTAL")
            .Font.Bold = True
            With .Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorDark1
                .TintAndShade = -0.149998474074526
                .PatternTintAndShade = 0
            End With
            .Borders.LineStyle = xlContinuous
        End With
        
        vResult = .Range("I2").Resize(dic.Count, 5)
        .Range("L2").Resize(dic.Count).NumberFormat = "@"
        For Each vKey In dic.Keys
            lRow = lRow + 1
            vResult(lRow, 1) = vKey
            vResult(lRow, 2) = dic(vKey)(0)
            vResult(lRow, 3) = dic(vKey)(1)
            vResult(lRow, 4) = dic(vKey)(2)
            vResult(lRow, 5) = dic(vKey)(3)
        Next vKey
        'Result range
        With .Range("I2").Resize(dic.Count, 5)
            .Value = vResult
            .Borders.LineStyle = xlContinuous
        End With
        .Columns("J:L").AutoFit
    End With
End Sub

M.
 
Upvote 0
Solution

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