Dim dicMain As Scripting.Dictionary 'main dictionary to hold column number and column dictionary
Dim dicColumn As Scripting.Dictionary 'column dictionary to hold criteria and count
Dim dicCurrent As Scripting.Dictionary 'currently referenced dictionary
Dim vColumns As Variant 'column numbers for which to return counts
Dim vCriteria As Variant 'criteria for which to return counts
Dim currentColumn As Long 'currently referenced column
Dim strCurrentCriteria As String 'currently referenced criteria
Dim objCurrentControl As Control 'currently referenced ActiveX control
vColumns = Array(2, 4, 8) 'columns for which to return counts
vCriteria = Array("S", "W", "J", "P") 'letters for which to return counts
'create an instance of a dictionary object for main dictionary
Set dicMain = New Scripting.Dictionary
'set dictinary to case-insensitive comparison mode
dicMain.CompareMode = TextCompare
'loop through each specified column
For i = 0 To UBound(vColumns)
'create dictionary for column to hold criteria/count pair
Set dicColumn = New Scripting.Dictionary
'set to case-insensitive comparison mode
dicColumn.CompareMode = TextCompare
'initialize count for each criteria to zero
For j = 0 To UBound(vCriteria)
dicColumn(vCriteria(j)) = 0
Next j
'add column number and corresponding dictionary to main dictionary
dicMain.Add Key:=vColumns(i), Item:=dicColumn
Next i
'loop through each row within listbox to return counts for specified columns
With ListBox1
For i = 0 To .ListCount - 1
For j = 0 To UBound(vColumns)
currentColumn = vColumns(j)
strCurrentCriteria = .List(i, currentColumn - 1)
If Not IsError(Application.Match(strCurrentCriteria, vCriteria, 0)) Then
Set dicCurrent = dicMain(currentColumn)
dicCurrent(strCurrentCriteria) = dicCurrent(strCurrentCriteria) + 1
End If
Next j
Next i
End With
'loop through each textbox and assign their respective counts
For Each objCurrentControl In Me.Controls
If TypeName(objCurrentControl) = "TextBox" Then
currentColumn = Split(objCurrentControl.Tag, ";")(0)
strCurrentCriteria = Split(objCurrentControl.Tag, ";")(1)
objCurrentControl.Value = dicMain(currentColumn)(strCurrentCriteria)
End If
Next objCurrentControl