Expanding table via Scripting dictionary

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,850
Office Version
  1. 365
  2. 2019
  3. 2013
  4. 2007
Platform
  1. Windows
Hi All
I've been trying to improve my knowledge of Dictionaries and have been using this code kindly submitted by @Rick Rothstein some time ago.
How can I expand the data so that it also sums column "C" as well. Any help would be much appreciated
VBA Code:
Sub Consolidate()
  Dim R As Long, Data As Variant
  Data = Range("A2", Cells(Rows.Count, "B").End(xlUp))
  With CreateObject("Scripting.Dictionary")
    For R = 1 To UBound(Data)
      .Item(Data(R, 1)) = .Item(Data(R, 1)) + Data(R, 2)
     Next R
    Range("D2").Resize(.Count) = Application.Transpose(.Keys)
    Range("E2").Resize(.Count) = Application.Transpose(.Items)
  End With
End Sub
 
Try the next one, no condition with a single cycle, I think it's a bit faster.

VBA Code:
Sub Consolidate4_d()
  Dim R As Long, Data As Variant, dic As Object, dic2 As Object
  Data = Range("A2", Cells(Rows.Count, "C").End(xlUp))
  Set dic = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  For R = 1 To UBound(Data)
    dic(Data(R, 1)) = dic(Data(R, 1)) + Data(R, 2)
    dic2(Data(R, 1)) = dic2(Data(R, 1)) + Data(R, 3)
  Next
  Range("D2").Resize(dic.Count, 2).Value = Application.Transpose(Array(dic.keys, dic.items))
  Range("F2").Resize(dic.Count, 1).Value = Application.Transpose(dic2.items)
End Sub
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
@DanteAmor
Yep, you are correct, your latest is about 37% faster than that of Joes....
Scripting Dictionary Example multi.xls
HIJK
2DanteAmor=0.34765625seconds
3Peter_SSs=0.421875seconds
4Fluff=0.3125seconds
5JoeMo=0.2890265seconds
6DanteAmor V2=0.18359375seconds
Sum Data
 
Upvote 0
Hi All
I've been trying to improve my knowledge of Dictionaries and have been using this code kindly submitted by @Rick Rothstein some time ago.
How can I expand the data so that it also sums column "C" as well. Any help would be much appreciated
@Michael M I am confused. You asked for 'sum of column C'. All of the great code examples here paste the entire Column C. None of them 'sum' the C Column as you requested.
 
Upvote 0
I am confused. You asked for 'sum of column C'. All of the great code examples here paste the entire Column C. None of them 'sum' the C Column as you requested.
It is implicit from the original code provided, which sums column B per unique item in column A, that the request was to also sum column C per unique item in column A
 
Upvote 0
@johnnyL
No, they ALL work as requested....some slightly faster than others !!
It may depend on what your data setup is...If all the names in Col "A" are unique then col "C" will be the same as the original table
 
Upvote 0
I guess I use different terminology. I don't see any 'summing' of any columns here in this thread. My apologies!
 
Upvote 0
Ok, try it with this small data set... you will see the result of summed cells in adjacent columns
Scripting Dictionary Example multi.xls
ABC
1CustomerIDAmountItems
2A1$ 8,701158
3B1$ 9,14271
4C1$ 5,11535
5D1$ 2,40023
6A1$ 6,50042
7B1$ 5,00029
8B1$ 4,56750
Sum Data
 
Upvote 0
I don't see any 'summing' of any columns here in this thread.
Check all the codes, they have the following instruction to load into a Data matrix 3 columns.

Data = Range("A2", Cells(Rows.Count, "C").End(xlUp))

Column 3 represents column C.
All codes have a loop that sums the values in column C into a dictionary that has the unique values.

.Item(Data(R, 1)) = .Item(Data(R, 1)) + Data(R, 3)

Tmp(1) = Tmp(1) + Data(R, 3)

.Item(Data(R, 1)) = Array(.Item(Data(R, 1))(0) + Data(R, 2), .Item(Data(R, 1))(1) + Data(R, 3))

dic2(Data(R, 1)) = dic2(Data(R, 1)) + Data(R, 3)

The codes have different structure and @Michael M was checking which one is the fastest. :cool:
 
Upvote 0
Ok, try it with this small data set... you will see the result of summed cells in adjacent columns
Scripting Dictionary Example multi.xls
ABC
1CustomerIDAmountItems
2A1$ 8,701158
3B1$ 9,14271
4C1$ 5,11535
5D1$ 2,40023
6A1$ 6,50042
7B1$ 5,00029
8B1$ 4,56750
Sum Data
Ahaaaa! I didn't have that sample you provided there, so I just made up totally random, Non repeating values for Columns A - C and tested the codes being offered here, shocker, nothing was being summed because all values were different. :rollseyes

Now that I know how the game is being played, lemme see what I can come up with.

Thank you all for such a fun site!
 
Upvote 0
Yeah...the ..Non repeating values.....will make the dictionary process fairly redundant.
My sample ashould show how the process works...:giggle:
 
Upvote 0

Forum statistics

Threads
1,215,750
Messages
6,126,663
Members
449,326
Latest member
asp123

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