VBA Dic Key with multiple Items in differnt columns

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,177
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Is there another way to approch this. I am trying to count multiple items in different columns with one key. right now I am using,

dic.Add cell.Value, cell.Value & "|" & WorksheetFunction.CountIfs(Range("A:A"), cell.Value, Range("B:B"), "<>") & "|" & WorksheetFunction.CountIfs(Range("A:A"), cell.Value, Range("C:C"), "<>")

Any help would be great! Thanks.

VBA Dic Mult Items in Columns.xlsm
ABCDEFGHIJ
1SchoolStudentsParentsIDStudentParent
2111A11163
3111B22240
4111C33321
5111DA44422
6111E55531
7222F
8222G
9222H
10333I
11333J
12444K
13444L
14444A
15111W
16111Q
17222G
18333U
19444Y
20555M
21555M
22555X
23555C
24111V
Sheet2



VBA Code:
Option Explicit
Sub Countit()
Dim dic As New Dictionary
Dim k As Variant
Dim cell As Range
Dim lastrow As Long
Dim i As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each cell In Range("A2:A" & lastrow)
        If Not dic.Exists(cell.Value) Then
            dic.Add cell.Value, cell.Value & "|" & WorksheetFunction.CountIfs(Range("A:A"), cell.Value, Range("B:B"), "<>") & "|" & WorksheetFunction.CountIfs(Range("A:A"), cell.Value, Range("C:C"), "<>")
        End If
    Next
Range("H1").Value = "ID"
Range("I1").Value = "Student"
Range("J1").Value = "Parent"
i = 2
    For Each k In dic.Items
        Cells(i, "H").Resize(, 3).Value = Split(k, "|")
        i = i + 1
    Next k
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about
VBA Code:
Sub Countit()
Dim dic As New Dictionary
Dim k As Variant, tmp As Variant
Dim cell As Range
Dim lastrow As Long
Dim i As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each cell In Range("A2:A" & lastrow)
        If Not dic.Exists(cell.Value) Then
            dic.Add cell.Value, Array(-(cell.Offset(, 1) <> ""), -(cell.Offset(, 2) <> ""))
         Else
            tmp = dic(cell.Value)
            tmp(0) = tmp(0) - (cell.Offset(, 1) <> "")
            tmp(1) = tmp(1) - (cell.Offset(, 2) <> "")
            dic(cell.Value) = tmp
        End If
    Next
Range("H1").Value = "ID"
Range("I1").Value = "Student"
Range("J1").Value = "Parent"
Range("H2").Resize(dic.Count).Value = Application.Transpose(dic.Keys)
Range("I2").Resize(dic.Count, 2).Value = Application.Index(dic.Items, 0, 0)
End Sub
 
Upvote 0
Thanks Fluff. That is right on!! I appreciate all of your help and admire your posts! Thanks again!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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