VBA - Search duplicate values, sum quantities, and concatenate a string

LearnNewThings

New Member
Joined
Aug 27, 2019
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I have a data set that is sent to me from one of my suppliers, I have attached a sample of the data here. In this data set, there is one column with customer PN (Column B), 3 columns with quantities (Columns C, D, & E), and a column with purchase order information (Column F). Throughout the data, there are many instances where there are duplicate rows of the customer PN (Column B). I would like to sum up each QTY column for all given duplicated Customer PNs, and concatenate the PO information with a carriage return. I have manually provided an example of what I am trying to achieve below the yellow line. The data set is ~1000 lines long, so a manual approach is not ideal.



A couple of notes regarding the data:

  • Not all Customer PN have duplicate values
  • There could be more than 2 rows with the same Customer PN
  • Not all values in either of the three QTY values are greater than 0
  • Not all duplicate Customer PN will have anything in the PO Info column
  • I already have VBA scripts to manipulate some of the data, so adding more is not a big deal

Test Data.xlsx
ABCDEF
1PART NUMBERCUSTOMER P/NON HANDQTY REQON ORDERPO INFO
251-5737160-0004720.00130.00105.00702840530201 105 2/10/2022 TECH ETCH, INC. CONFIRMED
351-6213160-0004710.000.0010.00702840531231 10 2/10/2022 TECH ETCH, INC. CONFIRMED
451-5735160-0004627.00130.00105.00702840530101 105 2/10/2022 TECH ETCH, INC. CONFIRMED
551-6132160-000466.000.000.00
654-4375430-000840.000.000.00
754-4499430-0008490.00168.0080.00702841990601 80 2/16/2022 M&K ENGINEERING INC CONFIRMED
854-4416430-000870.000.000.00
954-4491430-000870.000.000.00
1054-4372430-001082.000.000.00
1154-4496430-0010894.00168.0075.00702841990301 75 2/16/2022 M&K ENGINEERING INC CONFIRMED
1254-4370431-000102.000.000.00
1354-4495431-0001022.00168.00150.00702841820101 150 2/14/2022 PDF CO CONFIRMED
1451-5729437-0000425.68260.00240.00702840550701 240 2/14/2022 MCMASTER-CARR SUPPLY CO. CONFIRMED
1551-6110437-000040.000.000.00
1651-5716437-0000534.308.500.00
1754-4451437-000050.000.000.00
1882-849440-000140.000.000.00
1993-3735440-000140.000.000.00
2082-848440-000150.000.000.00
2193-3734440-000150.000.000.00
2282-857440-000350.000.000.00
2393-3815440-000350.000.000.00
24
25
26
27CUSTOMER P/NON HANDQTY REQON ORDERPO INFO
28160-0004730.00130.00115.00702840530201 105 2/10/2022 TECH ETCH, INC. CONFIRMED 702840531231 10 2/10/2022 TECH ETCH, INC. CONFIRMED
Sheet2
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this, data on sheet2, results on sheet3.

VBA Code:
Sub sum_quantities()
  Dim a As Variant
  Dim dic As Object
  Dim i As Long, j As Long
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = Sheets("Sheet2").Range("A2:F" & Sheets("Sheet2").Range("B" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  
  For i = 1 To UBound(a, 1)
    If Not dic.exists(a(i, 2)) Then
      j = j + 1
      dic(a(i, 2)) = j
    End If
    j = dic(a(i, 2))
    b(j, 1) = a(i, 2)
    b(j, 2) = b(j, 2) + a(i, 3)
    b(j, 3) = b(j, 3) + a(i, 4)
    b(j, 4) = b(j, 4) + a(i, 5)
    b(j, 5) = IIf(b(j, 5) = "", a(i, 6), b(j, 5) & vbCr & a(i, 6))
  Next
  Sheets("Sheet3").Range("A2").Resize(UBound(b, 2), 5).Value = b
End Sub
 
Upvote 0
Solution
DanteAmor,

This worked well for some of the data, but it did not perform this on all of the data. It ended at Customer PN 431-00010. When I applied it to my larger data set, it only performed it on a limited set of data instead of the whole set. How do I expand this to a data set that is dynamic in size (row count)? Additionally, the PO text did not have a carriage return until I double-clicked in the cell itself (then it split where it should). Is there a way to have the carriage return be visible without needing to double-click in the respective cell?

Finally, would it be too much to ask if you could add some comments explaining the logic you have proposed? This is a completely new approach that I am unfamiliar with and would LOVE to learn what is driving this.

Thanks so much!!

- Rich
 
Upvote 0
This worked well for some of the data, but it did not perform this on all of the data. It ended at Customer PN 431-00010.
Check that the data does not have spaces before or after inside the cell.


When I applied it to my larger data set
How many records do you have?

How do I expand this to a data set that is dynamic in size (row count)?
The code works down to the last cell with data from column B.
 
Upvote 0
Check that the data does not have spaces before or after inside the cell.
There are no spaces anywhere in the cells of column B
How many records do you have?
For the last export, there were 764 records. However, this size can change.
The code works down to the last cell with data from column B.
It didn't work past row 13 in my sample data (which resulted in 6 merged rows. When I applied it to my larger data set, it ended at row 7 (which resulted in 6 rows). I have included screenshots of example data and the output that resulted from running the code. I have also included an expanded data set, though it is not the complete set.


12202021.xlsm
ABCDEF
1PART NUMBERCUSTOMER P/NON HANDQTY REQON ORDERPO INFO
251-5735160-0004627130105702840530101 105 2/10/2022 TECH ETCH, INC. CONFIRMED
351-6132160-00046600
454-4375430-00084000
554-4499430-000849016880702841990601 80 2/16/2022 M&K ENGINEERING INC
654-4374430-00085000
754-4498430-000859516875702841990501 75 2/16/2022 M&K ENGINEERING INC
854-4373430-00086000
954-4497430-000869516875702841990401 75 2/16/2022 M&K ENGINEERING INC
1054-4416430-00087000
1154-4491430-00087000
1254-4415430-00088000
1354-4490430-000884260256702813010501 8 12/22/2021 WROBEL ENGINEERING CO INC CONFIRMED 702813010502 13 12/22/2021 WROBEL ENGINEERING CO INC CONFIRMED 702841861801 125 3/21/2022 WROBEL ENGINEERING CO INC CONFIRMED 702841861802 110 3/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
1454-4414430-00089000
1554-4489430-00089055702730520901 5 1/14/2022 WROBEL ENGINEERING CO INC CONFIRMED
1654-4398430-00090000
1754-4485430-000900130130702813471001 5 1/19/2022 WROBEL ENGINEERING CO INC CONFIRMED 702813471002 5 1/19/2022 WROBEL ENGINEERING CO INC CONFIRMED 702841861301 120 2/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
1854-4396430-00091000
1954-4483430-000910130130702813470901 5 1/19/2022 WROBEL ENGINEERING CO INC CONFIRMED 702813470902 5 1/19/2022 WROBEL ENGINEERING CO INC CONFIRMED 702841861101 120 2/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
2054-4394430-00094000
2154-4481430-0009425260235702841860901 235 2/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
2254-4402430-00097000
2354-4877430-000970130130702845060101 25 12/22/2021 WROBEL ENGINEERING CO INC CONFIRMED 702845060201 105 2/11/2022 WROBEL ENGINEERING CO INC CONFIRMED
2454-4413430-00099000
2554-4488430-000995130125702813471302 5 12/29/2021 WROBEL ENGINEERING CO INC CONFIRMED 702841861601 120 2/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
2654-4393430-00100000
2754-4440430-0010015130115702841860501 115 2/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
2854-4391430-00102000
2954-4480430-0010224130106702841860801 106 2/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
3054-4404430-00103000
3154-4486430-0010310130120702841861401 120 2/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
3254-4411430-00104000
3354-4487430-001040130130702813471201 25 1/19/2022 WROBEL ENGINEERING CO INC CONFIRMED 702841861501 105 2/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
3454-4366430-00105000
3554-4479430-001051130129702813470603 9 12/30/2021 WROBEL ENGINEERING CO INC CONFIRMED 702841860701 120 2/28/2022 WROBEL ENGINEERING CO INC CONFIRMED
3654-4372430-00108200
3754-4496430-001089416875702841990301 75 2/16/2022 M&K ENGINEERING INC
3851-5753431-0000541168130702840600101 130 2/14/2022 RENISHAW INC. CONFIRMED
3954-4371431-00005000
4054-4370431-00010200
4154-4495431-0001022168150702841820101 150 2/14/2022 PDF CO CONFIRMED
4254-4389431-00013000
4354-4511431-0001333170150702841840301 150 3/2/2022 ORION INDUSTRIES, INC. CONFIRMED
4454-4385432-00052000
4554-4506432-0005235170135702841920301 135 2/2/2022 STRATASYS DIRECT, INC. CONFIRMED
4654-4369432-00057000
4754-4494432-000579516875702841990201 75 2/16/2022 M&K ENGINEERING INC
4854-4368432-00058000
4954-4493432-0005845168125702841920201 125 1/14/2022 STRATASYS DIRECT, INC. CONFIRMED
5054-4367432-00077000
5154-4492432-0007794168100702841990101 100 2/16/2022 M&K ENGINEERING INC
5251-5729437-0000425.68260240702840550701 240 2/14/2022 MCMASTER-CARR SUPPLY CO. CONFIRMED
5351-6110437-00004000
5451-5716437-0000534.38.50
5554-4451437-00005000
5682-849440-00014000
5793-3735440-00014000
5882-848440-00015000
5993-3734440-00015000
6082-857440-00035000
6193-3815440-00035000
6251-5711445-000441000
6351-5824445-0004455365.5914325702840560101 325 2/14/2022 HARDWARE SPECIALTY INC CONFIRMED
64801-1087810-00008000
65801-1121810-00008000
66801-1096810-00011000
67801-1120810-00011000
68801-1097810-00013000
69801-1122810-00013000
70800-1824820-0002701766501667570000 6 6/30/2021
71801-1086820-00027000
7293-3784820-00027000
7393-3812820-00027000
74800-1825820-0002801757501661990000 1 6/30/2021 501670880000 6 6/30/2021
75801-1085820-00028000
7693-3782820-00028000
7793-3813820-00028000
78801-1095820-00032000
79801-1119820-00032000
80800-1823820-0003301366501671040000 6 6/28/2021
81801-1094820-00033100
8293-3811820-00033000
83801-1093820-00040000
84801-1118820-00040000
85801-1092820-00041000
86801-1117820-00041000
87800-1822820-0004301300
88801-1091820-00043000
8993-3810820-00043000
90801-1084820-00044000
91801-1115820-00044000
9293-3814820-00044000
93800-1806820-00045000
94800-1821820-0004501300
95801-1090820-00045000
9693-3809820-00045000
97801-1089820-00047000
98801-1116820-00047000
99600-3098830-000101917010501675980000 10 12/30/2021
10092-416830-00010000
101600-3103830-00011201685501674830000 5 12/10/2021
10292-417830-00011000
10390-3753830-00011-FAB70650
10490-3834830-00011-FAB080145702841070101 145 12/27/2021 NCAB GROUP USA CONFIRMED
105600-3104830-0001201300
10692-422830-00012000
107600-3099830-000131817010501675990000 10 12/30/2021
10892-415830-00013000
109600-3105830-00014101300
11092-421830-00014000
111600-3100830-00015251700
11292-419830-00015000
113600-3101830-00016251700
11492-418830-00016000
115600-3102830-00017101300
11692-420830-00017000
11794-308840-00010000
11894-320840-00010101300
11952-1714850-00041000
12052-1761850-0004110130120702844811001 65 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844811002 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
12152-1713850-00042000
12252-1760850-0004215130115702844810901 60 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844810902 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
12352-1712850-00044000
12452-1759850-0004410130120702844810801 65 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844810802 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
12552-1711850-00045000
12652-1758850-0004510130120702844810701 65 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844810702 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
12752-1710850-00046000
12852-1757850-0004610130120702844810601 65 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844810602 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
12952-1709850-00047000
13052-1756850-0004710130120702844810501 65 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844810502 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
13152-1719850-00049000
13252-1740850-0004910130120702844860101 65 1/14/2022 FOURSTAR CONNECTIONS INC 702844860102 55 2/16/2022 FOURSTAR CONNECTIONS INC
13352-1708850-00050000
13452-1755850-0005010130120702844810401 65 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844810402 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
13552-1707850-00051000
13652-1754850-0005110130120702844810301 65 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844810302 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
13752-1706850-00052000
13852-1753850-000520130130702844811701 65 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844811702 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702812240201 10 3/21/2022 SCOTT ELECTRONICS INC CONFIRMED
13952-1705850-00054000
14052-1752850-000547513055702844810201 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
14152-1704850-00058000
14252-1751850-0005810130120702844861601 65 1/28/2022 FOURSTAR CONNECTIONS INC 702844861602 55 2/28/2022 FOURSTAR CONNECTIONS INC
14352-1702850-00060000
14452-1750850-0006010130120702844860701 65 1/28/2022 FOURSTAR CONNECTIONS INC 702844860702 55 2/28/2022 FOURSTAR CONNECTIONS INC
14552-1717850-00061000
14652-1763850-0006110130120702844860901 65 1/14/2022 FOURSTAR CONNECTIONS INC 702844860902 55 2/16/2022 FOURSTAR CONNECTIONS INC
14752-1700850-00063000
14852-1748850-0006310130120702844861401 65 1/28/2022 FOURSTAR CONNECTIONS INC 702844861402 55 2/28/2022 FOURSTAR CONNECTIONS INC
14952-1699850-00064000
15052-1747850-0006410130120702844861101 65 1/28/2022 FOURSTAR CONNECTIONS INC 702844861102 55 2/28/2022 FOURSTAR CONNECTIONS INC
15152-1715850-00065000
15252-1762850-0006510130120702844811801 65 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844811802 55 2/28/2022 SCOTT ELECTRONICS INC CONFIRMED
15352-1698850-00070000
15452-1746850-000707513055702844860501 55 2/28/2022 FOURSTAR CONNECTIONS INC
15552-1695850-00073000
15652-1742850-0007310130120702844861501 65 1/28/2022 FOURSTAR CONNECTIONS INC 702844861502 55 2/28/2022 FOURSTAR CONNECTIONS INC
15752-1694850-00074000
15852-1743850-000748130125702844810101 25 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED 702844810102 100 1/28/2022 SCOTT ELECTRONICS INC CONFIRMED
159800-1799900-00024000
160800-1828900-0002401788501670490000 8 7/16/2021
16193-3786900-00024000
16293-3817900-00024000
163800-1798900-00025000
164800-1827900-0002501388501670960000 8 7/16/2021
16593-3816900-00025000
166800-1797900-00034000
167800-1826900-0003401322501667580000 2 7/16/2021
16854-4782FAB-20-00094500
16954-4845FAB-20-000940170170702832300101 25 12/13/2021 Cytrellis Biosystems, Inc 702840900201 145 3/1/2022 Cytrellis Biosystems, Inc
17054-4769FAB-20-00097000
17154-4844FAB-20-0009725170145702840900101 145 3/1/2022 Cytrellis Biosystems, Inc
17293-4081LBL-00029000
17393-4119LBL-00029101300
17493-3670WI-00103000
17593-3777WI-00103000
17693-3671WI-00117000
17793-3776WI-00117000
17893-3674WI-00120000
17993-4052WI-00120000
Sheet1
 

Attachments

  • Example Data.png
    Example Data.png
    97.7 KB · Views: 7
  • Example Output.png
    Example Output.png
    24.6 KB · Views: 7
Upvote 0
My fault.

Change this:
Sheets("Sheet3").Range("A2").Resize(UBound(b, 2), 5).Value = b

For this:
Sheets("Sheet3").Range("A2").Resize(UBound(b, 1), 5).Value = b
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0
Same Idea but...
VBA Code:
Sub sum_quantities()
    Dim a As Variant
    Dim dic As Object
    Dim i As Long, j As Long
    a = Sheets("Sheet2").Range("A2:F" & Sheets("Sheet2").Range("B" & Rows.Count).End(3).Row).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(a)
            If Not .exists(a(i, 2)) Then
                .Add a(i, 2), Array(a(i, 2), a(i, 3), a(i, 4), a(i, 5), a(i, 6))
            Else
                w = .Item(a(i, 2))
                w(1) = w(1) + a(i, 3):   w(2) = w(2) + a(i, 4)
                w(3) = w(3) + a(i, 5):   w(4) = w(4) & vbLf & a(i, 6)
                .Item(a(i, 2)) = w
            End If
            Set w = Nothing
        Next
        Sheets("Sheet3").Range("A2").Resize(.Count, 5).Value = Application.Index(.items, 0, 0)
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

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