How can I replicate the following code with VBA collections rather than dictionaries? (I cannot access from Tools / References... / "Microsoft Scripting Runtime" at work)
The idea is to clean up any unused styles in VBA (https://stackoverflow.com/a/8933399/19794809)
Public Sub DropUnusedStyles()
Dim styleObj As Style
Dim rngCell As Range
Dim wb As Workbook
Dim wsh As Worksheet
Dim str As String
Dim iStyleCount As Long
Dim dict As New Scripting.Dictionary
Set wb = ThisWorkbook ' choose this module's workbook
MsgBox "BEGINNING # of styles in workbook: " & wb.Styles.Count
' dict := list of styles
For Each styleObj In wb.Styles
str = styleObj.NameLocal
iStyleCount = iStyleCount + 1
Call dict.Add(str, 0) ' First time: adds keys
Next styleObj
For Each wsh In wb.Worksheets
If wsh.Visible Then
For Each rngCell In wsh.UsedRange.Cells
str = rngCell.Style
dict.Item(str) = dict.Item(str) + 1 ' This time: counts occurrences
Next rngCell
End If
Next wsh
' Try to delete unused styles
Dim aKey As Variant
On Error Resume Next ' wb.Styles(aKey).Delete may throw error
For Each aKey In dict.Keys
' display count & stylename
' e.g. "24 Normal"
Debug.Print dict.Item(aKey) & vbTab & aKey
If dict.Item(aKey) = 0 Then
' Occurrence count (Item) indicates this style is not used
Call wb.Styles(aKey).Delete
If Err.Number <> 0 Then
Debug.Print vbTab & "^-- failed to delete"
Err.Clear
End If
Call dict.Remove(aKey)
End If
Next aKey
MsgBox "ENDING # of style in workbook: " & wb.Styles.Count
End Sub