scripting.dictionary issue with 3rd colum values

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
Having an issue with summing the 3rd column.

This relates to Post 11.

I have the following code which takes multiple 3 column ranges, each 3 column range is separated with a blank column, and it takes the first column, keeps track of each unique value, sums the corresponding 2nd column, and for the 3rd column it keeps a counter for each time the 1st column value was found: Thanks to @Fluff !

VBA Code:
Sub JonnyL1()
   Dim Cl As Range
   Dim Tmp As Variant
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("D3:V8").SpecialCells(xlConstants, xlTextValues)
         If Not .Exists(Cl.Value) Then
            .Item(Cl.Value) = Array(Cl.Offset(, 1).Value, 1)
         Else
            Tmp = .Item(Cl.Value)
            Tmp(0) = Tmp(0) + Cl.Offset(, 1).Value
            Tmp(1) = Tmp(1) + 1
            .Item(Cl.Value) = Tmp
         End If
      Next Cl
      Range("A2").Resize(.Count, 1).Value = Application.Transpose(.Keys)
      Range("B2").Resize(.Count, 2).Value = Application.Index(.Items, 0)
      Range("A1").Resize(.Count, 3).Sort Range("A1"), xlAscending, , , , , , xlYes
   End With
End Sub

Now instead of the 3rd column being a counter, I want it to be a sum of all of the 3rd column corresponding values to the first column. I tried the following slight alteration of the 1st code:

VBA Code:
Sub JonnyL2()
   Dim Cl As Range
   Dim Tmp As Variant
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("D3:V8").SpecialCells(xlConstants, xlTextValues)
         If Not .Exists(Cl.Value) Then
            .Item(Cl.Value) = Array(Cl.Offset(, 1).Value, 1)
         Else
            Tmp = .Item(Cl.Value)
            Tmp(0) = Tmp(0) + Cl.Offset(, 1).Value
'            Tmp(1) = Tmp(1) + 1
            Tmp(1) = Tmp(1) + Cl.Offset(, 2).Value
            .Item(Cl.Value) = Tmp
         End If
      Next Cl
      Range("A2").Resize(.Count, 1).Value = Application.Transpose(.Keys)
      Range("B2").Resize(.Count, 2).Value = Application.Index(.Items, 0)
      Range("A1").Resize(.Count, 3).Sort Range("A1"), xlAscending, , , , , , xlYes
   End With
End Sub

But the 3rd column is not giving the correct totals.

Book1
ABCDEFGHIJKLMNOPQRSTUV
1ColourQty Ordered
2Black1414
3Blue32Red146Green83Blue13Brown410Black25
4Brown41Blue21Purple27Red42Green61
5Green195Green52Orange61Orange47
6Orange102Red05Purple83
7Purple108Yellow49
8Red1812Black1213
9Yellow41
10
11
Sheet1



It should be:

Book1
ABCDEFGHIJKLMNOPQRSTUV
1ColourQty Ordered
2Black1418
3Blue34Red146Green83Blue13Brown410Black25
4Brown410Blue21Purple27Red42Green61
5Green196Green52Orange61Orange47
6Orange108Red05Purple83
7Purple1010Yellow49
8Red1813Black1213
9Yellow49
10
Sheet1


I thought the slight change I made would yield what I wanted, but it doesn't. :(

Any ideas?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You need
VBA Code:
.Item(Cl.Value) = Array(Cl.Offset(, 1).Value, Cl.Offset(, 2).Value)
 
Upvote 0
Solution
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,525
Messages
6,125,325
Members
449,218
Latest member
Excel Master

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