VBA to merge duplicate rows and sum values in certain column

mwstorms21

New Member
Joined
Sep 13, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,
I am new to this forum, but ran across MickG's code for using VBA to merge duplicate rows and sum values in certain column. I was hoping someone might be able to help me edit this code to suit my needs.
The original thread is here: VBA to merge duplicate rows and sum values in certain column

The following data (headers titles) starts on row 12 of my spreadsheet. (row 13 is the first row that I want to process)

I would like to identify unique values in column 5 ("Description"), delete duplicate rows, and sum column 6 ("Quantity") while leaving the other columns (1,2,3,4,7,8,9,10) unchanged.

Thank you so much for your help and please let me know if there is any other information I need to provide.

LinePrice ListPart NumberManufacturerDescriptionQuantityPro-Rated PriceExtended PriceStart DateEnd Date
1PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 365 days unlimited Verizon data plan (DTL at 15GB)1$ 400.00$ 400.009/11/20209/11/2021
2PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 365 days unlimited Verizon data plan (DTL at 15GB)1$ 400.00$ 400.009/12/20209/11/2021
3PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 365 days unlimited Verizon data plan (DTL at 15GB)1$ 400.00$ 400.009/12/20209/11/2021
4PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 283 days unlimited Verizon data plan (DTL at 15GB)1$ 300.00$ 300.0012/3/20209/11/2021
5PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 283 days unlimited Verizon data plan (DTL at 15GB)1$ 300.00$ 300.0012/3/20209/11/2021
6PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 283 days unlimited Verizon data plan (DTL at 15GB)1$ 300.00$ 300.0012/3/20209/11/2021
7PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 283 days unlimited Verizon data plan (DTL at 15GB)1$ 300.00$ 300.0012/3/20209/11/2021
8PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 162 days unlimited Verizon data plan (DTL at 15GB)1$ 200.00$ 200.004/3/20219/11/2021
9PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 162 days unlimited Verizon data plan (DTL at 15GB)1$ 200.00$ 200.004/3/20219/11/2021
10PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 162 days unlimited Verizon data plan (DTL at 15GB)1$ 200.00$ 200.004/3/20219/11/2021
11PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 155 days unlimited Verizon data plan (DTL at 15GB)1$ 150.00$ 200.004/10/20219/11/2021
12PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 137 days unlimited Verizon data plan (DTL at 15GB)1$ 100.00$ 100.004/28/20219/11/2021
13PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 137 days unlimited Verizon data plan (DTL at 15GB)1$ 100.00$ 100.004/28/20219/11/2021
14PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 137 days unlimited Verizon data plan (DTL at 15GB)1$ 100.00$ 100.004/28/20219/11/2021
15PRICE LIST 123PART1ManufacturerAAnnual Maintenance for Apple iPad including: 137 days unlimited Verizon data plan (DTL at 15GB)1$ 100.00$ 100.004/28/20219/11/2021
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I should also note that there are not always 15 rows that I want to process. The number of rows will vary between just a few to sometimes hundreds of rows. Thanks again!
 
Upvote 0
Try this:

VBA Code:
Sub merge_duplicate()
  Dim Rng As Range, Dn As Range, nRng As Range
  Dim n As Long, lr As Long, Txt As String

  Application.ScreenUpdating = False

  lr = Range("E" & Rows.Count).End(3).Row
  Set Rng = Range("E13:E" & lr)
  Set nRng = Range("E" & lr + 1)

  With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
 
    For Each Dn In Rng
      Txt = Dn.Value
      If Not .Exists(Txt) Then
        .Add Txt, Dn.Offset(, 1)
      Else
        .Item(Txt).Value = .Item(Txt).Value + Dn.Offset(, 1).Value
        Set nRng = Union(nRng, Dn)
      End If
    Next
 
    nRng.EntireRow.Delete
  End With

  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub merge_duplicate()
  Dim Rng As Range, Dn As Range, nRng As Range
  Dim n As Long, lr As Long, Txt As String

  Application.ScreenUpdating = False

  lr = Range("E" & Rows.Count).End(3).Row
  Set Rng = Range("E13:E" & lr)
  Set nRng = Range("E" & lr + 1)

  With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare

    For Each Dn In Rng
      Txt = Dn.Value
      If Not .Exists(Txt) Then
        .Add Txt, Dn.Offset(, 1)
      Else
        .Item(Txt).Value = .Item(Txt).Value + Dn.Offset(, 1).Value
        Set nRng = Union(nRng, Dn)
      End If
    Next

    nRng.EntireRow.Delete
  End With

  Application.ScreenUpdating = True
End Sub

DanteAmor,
That worked really well! Thank you so much!


However, there is one issue that I hope you can help me solve. Just below the last row of data that I want processed, there is a formula that Subtotals, a row that calculates a fee, and row that totals those. (see image). Below those rows, there is also some terms and conditions/payment info. I need to apply this formula without deleting the rows that contain the Subtotal, Fee, Total and any of the Terms and conditions info at the bottom.

The code works great! The only issue is it seems to delete the rows that contain my subtotal, fee, total, terms/quote details info (rows 59-64) in the attached image. Oddly enough, it seems to leave the terms and conditions below that alone. (rows 65-67 in the image)

Appreciate your help so much!!!
 

Attachments

  • Image1.JPG
    Image1.JPG
    83.4 KB · Views: 81
Upvote 0
Change this line:
VBA Code:
lr = Range("E" & Rows.Count).End(3).Row

For this:
Rich (BB code):
lr = Range("F" & Rows.Count).End(3).Row

And try again.
 
Upvote 0
You are awesome Dante! This works great!

I'm not sure if it's against forum rules. But I would really like to send you a gift via PayPal for your help. Mods please chime in if this is against the rules.

What a wonderful community this is!
 
Upvote 0
The rules don't allow it, just feedback is enough.
 
Upvote 0
Try this:

VBA Code:
Sub merge_duplicate()
  Dim Rng As Range, Dn As Range, nRng As Range
  Dim n As Long, lr As Long, Txt As String

  Application.ScreenUpdating = False

  lr = Range("E" & Rows.Count).End(3).Row
  Set Rng = Range("E13:E" & lr)
  Set nRng = Range("E" & lr + 1)

  With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
 
    For Each Dn In Rng
      Txt = Dn.Value
      If Not .Exists(Txt) Then
        .Add Txt, Dn.Offset(, 1)
      Else
        .Item(Txt).Value = .Item(Txt).Value + Dn.Offset(, 1).Value
        Set nRng = Union(nRng, Dn)
      End If
    Next
 
    nRng.EntireRow.Delete
  End With

  Application.ScreenUpdating = True
End Sub
I tried using this on a similar excel sheet. I am looking at column A and wanting to add quantity in column I, which is 8 columns away. When it runs, instead of adding the quantity numbers together, they are just putting them together. So if there are 3 of the same number with quantity 1, instead of making it quantity 3 it is coming up with quantity 111. What am I missing?

Dim Rng As Range, Dn As Range, nRng As Range
Dim n As Long, lr As Long, Txt As String

Application.ScreenUpdating = False

lr = Range("A" & Rows.Count).End(3).Row
Set Rng = Range("A2:A" & lr)
Set nRng = Range("A" & lr + 1)
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare

For Each Dn In Rng
Txt = Dn.Value
If Not .Exists(Txt) Then
.Add Txt, Dn.Offset(, 8)
Else
.Item(Txt).Value = .Item(Txt).Value + Dn.Offset(, 8).Value
Set nRng = Union(nRng, Dn)
End If
Next

nRng.EntireRow.Delete
End With
Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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