VBA Dictionary Multiple Items to Count Col B and Col C

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I need VBA assistance. I am using a dictionary to count data. My friend came up with the code below. I am just learning. I have my key as Column A I need to have the item be column B and C to put the output that i need. I am stuck. Any help would be appreciated.

VBA Dic Item Mult Cols.xlsm
ABC
1FacilitySanction Type (primary)Sanction Type (secondary)
214402010700
314781610710
414341910100
514341910649999
614141610100
714761110200
814761110200
914761110610
1014761110201054
1114112010690
1214252010650
1314791610660
1414421910761000
1514291910700
1614291910650
1714761110201010
1814761110201010
Sheet1



VBA Dic Item Mult Cols.xlsm
ABCDEFGHIJKLMN
11070107110101064102010611069106510661076999910541000
21440201000000000000
31478160100000000000
41434190011000000100
51414160010000000000
61476110020510000010
71411200000001000000
81425200000000100000
91479160000000010000
101442190000000001001
111429191000000100000
Sheet2

VBA Code:
Option Explicit
Public Sub main()
Dim lastrow As Long
Dim r As Range
lastrow = Cells(Rows.count, "A").End(xlUp).Row
Set r = Sheets("Sheet1").Range("A2:A" & lastrow)
Dim dic As New Dictionary
Dim dic2 As New Dictionary
Dim dic3 As New Dictionary
Dim dic4 As New Dictionary
Dim dic5 As New Dictionary
Dim xcell
Dim cell
Dim i As Long
Dim j As Long
Dim n As Long
Dim y
    For Each cell In r
        If Not dic.Exists(Trim(cell.Text)) Then
            dic.Add Trim(cell.Text), New Dictionary
        End If
        
        Set dic2 = dic(Trim(cell.Text))
        
        If Not dic2.Exists(Trim(cell.Offset(0, 1).Text)) Then
            dic2(Trim(cell.Offset(0, 1).Text)) = 0
        End If
        
        If Not dic5.Exists(Trim(cell.Offset(0, 2).Text)) Then
            dic5(Trim(cell.Offset(0, 2).Text)) = 0
        End If
        
        dic2(Trim(cell.Offset(0, 1).Text)) = dic2(Trim(cell.Offset(0, 1).Text)) + 1
        dic5(Trim(cell.Offset(0, 2).Text)) = dic5(Trim(cell.Offset(0, 2).Text)) + 1
        dic3(Trim(cell.Offset(0, 1).Text)) = Trim(cell.Offset(0, 1).Text)
        dic5(Trim(cell.Offset(0, 2).Text)) = Trim(cell.Offset(0, 2).Text)
    Next

j = 2
    For Each cell In dic3.Keys
        Sheet2.Cells(1, j).Value = cell
        j = j + 1
    Next

i = 2
    For Each cell In dic.Keys
        j = 2
        Sheet2.Cells(i, 1).Value = cell

Set dic4 = dic(cell)
Dim k
    
    For Each k In dic3.Keys
    Dim count As Integer
    count = 0
        If (dic4.Exists(k)) Then count = dic4(k)
        Sheet2.Cells(i, j).Value = count
        j = j + 1
    Next
        i = i + 1
    Next
End Sub
 
If you compare "Demo2bs" and "BSALV", both dictionaries provide the row and column for the array.
BSALV adds as key the attribute "facility" or "sanction", normally unnecessary because the 1st is 6 the other 4 characters long, but in case of ... .
PS. I added comments so that rookies can understand what happens.
BSALV! Thank you so much!!! I appreciate all of your help and guidance!!!
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
This is simplified and ready for an unknown number of columns.
With the dictionary, you know the row and column within the array
Demo2bs does not need to use Redim Preserve (one of the simplifications for your original procedure)​
as the maximum array size can be easily calculated …​
The reason why I used in post #16 this codeline : ReDim V(1 To UBound(W), 1 To UBound(W) * 2) …​
Or better the post #18 little optimization …​
 
Upvote 0
My point doesn't concern that redim, that's just a small detail, that one is quickly oversized, no problem.
If so then i only have to adjust this line
VBA Code:
ReDim Result(UBound(a), UBound(a) * ubound(a,2)
I have problems to read your mind, what 're doing in that macro, and i'm a skilled VBA-user.
 
Upvote 0
Maybe 'cause I can't see why you over complicated in your original procedure​
with Redim and Split(Replace(Join(Filter(dict.keys, rather than directly allocating the result array​
according to the OP has written : « I am just learning » …​
 
Upvote 0
okay, that was complicated.
That is now in row0 and column0 of that array.
It's better, you made your point for that one.
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

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