VBA, Arrais and Dict: combine duplicate rows and sum the values

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I have a table in the following range:

Code:
Worksheets(1).Range("A2:R" & lr2 + lr3 - 3)

Having column A as a reference point (it is a list of cities), if the same city is repeated, I've to sum all the value in column in order to produce only a row for any city.

Then overwrite the table.


Anyway, at the moment I've figured out only to sum the column B.

Code:
Set Rng = Worksheets(1).Range("A2:R" & lr2 + lr3 - 3)

Set Dic = CreateObject("Scripting.Dictionary")
arr = Rng.Value

For i = 1 To UBound(arr, 1)
    Dic(arr(i, 1)) = Dic(arr(i, 1)) + arr(i, 2)
Next

Application.ScreenUpdating = False

Rng.ClearContents

Rng.Range("A1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.keys)
Rng.Range("B1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.items)

Application.ScreenUpdating = True

How can I expand the same process to all columns until R?

Thank's in advance.
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about
Code:
Sub Nelson78()
   Dim Ary As Variant, Tmp As Variant, Cols As Variant
   Dim r As Long, c As Long
   
   With Worksheets(1).Range("A2:R" & lr2 + lr3 - 3)
      Ary = .Value2
      .ClearContents
   End With
   ReDim Cols(1 To UBound(Ary, 2) - 1)
   For c = 2 To UBound(Ary, 2)
      Cols(c - 1) = c
   Next c
   With CreateObject("scripting.dictionary")
      For r = 2 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Application.Index(Ary, r, Array(Cols))
         Else
            Tmp = .Item(Ary(r, 1))
            For c = 2 To UBound(Ary, 2)
               Tmp(c - 1) = Tmp(c - 1) + Ary(r, c)
            Next c
            .Item(Ary(r, 1)) = Tmp
         End If
      Next r
      Range("A1").Resize(.Count).Value = Application.Transpose(.Keys)
      Range("B1").Resize(.Count, 3).Value = Application.Index(.Items, 0, 0)
   End With
End Sub
 
Upvote 0
How about
Code:
Sub Nelson78()
   Dim Ary As Variant, Tmp As Variant, Cols As Variant
   Dim r As Long, c As Long
   
   With Worksheets(1).Range("A2:R" & lr2 + lr3 - 3)
      Ary = .Value2
      .ClearContents
   End With
   ReDim Cols(1 To UBound(Ary, 2) - 1)
   For c = 2 To UBound(Ary, 2)
      Cols(c - 1) = c
   Next c
   With CreateObject("scripting.dictionary")
      For r = 2 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Application.Index(Ary, r, Array(Cols))
         Else
            Tmp = .Item(Ary(r, 1))
            For c = 2 To UBound(Ary, 2)
               Tmp(c - 1) = Tmp(c - 1) + Ary(r, c)
            Next c
            .Item(Ary(r, 1)) = Tmp
         End If
      Next r
      Range("A1").Resize(.Count).Value = Application.Transpose(.Keys)
      Range("B1").Resize(.Count, 3).Value = Application.Index(.Items, 0, 0)
   End With
End Sub

Two problems:

1) I need to calculate sums until column R (after running the macro, I see it calculates until C);
2) it overwrites the header.

Thank's.
 
Last edited:
Upvote 0
Two problems:

1) I need to calculate sums until column R (after running the macro, I see it calculates until C);
2) it overwrites the header.

Thank's.

Point 1 could be fixed with:

Code:
Range("B1").Resize(.Count, [COLOR="#FF0000"]17[/COLOR]).Value = Application.Index(.Items, 0, 0)

instead of

Code:
Range("B1").Resize(.Count, 3).Value = Application.Index(.Items, 0, 0)
 
Upvote 0
How about
Code:
      Range("A2").Resize(.Count).Value = Application.Transpose(.Keys)
      Range("B2").Resize(.Count, UBound(Ary, 2) - 1).Value = Application.Index(.Items, 0, 0)
 
Upvote 0
How about
Code:
      Range("A2").Resize(.Count).Value = Application.Transpose(.Keys)
      Range("B2").Resize(.Count, UBound(Ary, 2) - 1).Value = Application.Index(.Items, 0, 0)

Now the header is ok, but the first city of the list is missing.
 
Upvote 0
How about
Code:
Sub Nelson78()
   Dim Ary As Variant, Tmp As Variant, Cols As Variant
   Dim r As Long, c As Long
   
   With Sheets(1).Range("A2:R" & lr2 + lr3 - 3)
      Ary = .Value2
      .ClearContents
   End With
   ReDim Cols(1 To UBound(Ary, 2) - 1)
   For c = 2 To UBound(Ary, 2)
      Cols(c - 1) = c
   Next c
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Application.Index(Ary, r, Array(Cols))
         Else
            Tmp = .Item(Ary(r, 1))
            For c = 2 To UBound(Ary, 2)
               Tmp(c - 1) = Tmp(c - 1) + Ary(r, c)
            Next c
            .Item(Ary(r, 1)) = Tmp
         End If
      Next r
      Range("A2").Resize(.Count).Value = Application.Transpose(.Keys)
      Range("B2").Resize(.Count, UBound(Ary, 2) - 1).Value = Application.Index(.Items, 0, 0)
   End With
End Sub
 
Upvote 0
How about
Code:
Sub Nelson78()
   Dim Ary As Variant, Tmp As Variant, Cols As Variant
   Dim r As Long, c As Long
   
   With Sheets(1).Range("A2:R" & lr2 + lr3 - 3)
      Ary = .Value2
      .ClearContents
   End With
   ReDim Cols(1 To UBound(Ary, 2) - 1)
   For c = 2 To UBound(Ary, 2)
      Cols(c - 1) = c
   Next c
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Application.Index(Ary, r, Array(Cols))
         Else
            Tmp = .Item(Ary(r, 1))
            For c = 2 To UBound(Ary, 2)
               Tmp(c - 1) = Tmp(c - 1) + Ary(r, c)
            Next c
            .Item(Ary(r, 1)) = Tmp
         End If
      Next r
      Range("A2").Resize(.Count).Value = Application.Transpose(.Keys)
      Range("B2").Resize(.Count, UBound(Ary, 2) - 1).Value = Application.Index(.Items, 0, 0)
   End With
End Sub

Yes, the tests I've executed so far are ok.

Thank's.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,686
Members
449,048
Latest member
81jamesacct

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