Public Sub Check_and_Hide_Rows2()
Dim VisibleNamesDict As Object 'Dictionary
Dim table As ListObject
Dim visibleAreas As Areas
Dim areaRange As Range
Dim thisAreaRange As Variant
Dim r As Long, i As Long
Dim thisRow As cRow
Dim Slicer1SC As SlicerCache
Dim Slicer2SC As SlicerCache
Dim slItem As SlicerItem
Dim FoundSlicer1Count As Long, FoundSlicer2Count As Long
Dim Slicer1Count As Long, Slicer2Count As Long
Dim numRows As Long
Dim Slicer1SelectedItems() As String, numSlicer1SelectedItems As Long
Dim Slicer2SelectedItems() As String, numSlicer2SelectedItems As Long
Set Slicer1SC = ThisWorkbook.SlicerCaches("Slicer_Skillset") 'CHANGE THIS SLICER NAME
Set Slicer2SC = ThisWorkbook.SlicerCaches("Slicer_Score") 'CHANGE THIS SLICER NAME
Set table = Slicer1SC.ListObject
Slicer1SC.ListObject.AutoFilter.ApplyFilter
Slicer2SC.ListObject.AutoFilter.ApplyFilter
'Set VisibleNamesDict = New Dictionary
Set VisibleNamesDict = CreateObject("Scripting.Dictionary")
Set visibleAreas = Nothing
On Error Resume Next
Set visibleAreas = table.DataBodyRange.SpecialCells(xlCellTypeVisible).Areas
On Error GoTo 0
If Not visibleAreas Is Nothing Then
'Create VisibleNamesDict dictionary with 1 entry for each unique visible name
For Each areaRange In visibleAreas
thisAreaRange = areaRange.Value
For r = 1 To UBound(thisAreaRange)
Set thisRow = New cRow
thisRow.NameColA = thisAreaRange(r, 1)
If Not VisibleNamesDict.Exists(thisRow.NameColA) Then
thisRow.Slicer1Col_List = Trim(thisAreaRange(r, 2)) & ","
thisRow.Slicer2Col_List = Trim(thisAreaRange(r, 3)) & ","
VisibleNamesDict.Add thisRow.NameColA, thisRow
Else
Set thisRow = VisibleNamesDict(thisRow.NameColA)
If InStr(1, "," & thisRow.Slicer1Col_List, "," & Trim(thisAreaRange(r, 2)) & ",") = 0 Then
thisRow.Slicer1Col_List = thisRow.Slicer1Col_List & Trim(thisAreaRange(r, 2)) & ","
End If
If InStr(1, "," & thisRow.Slicer2Col_List, "," & Trim(thisAreaRange(r, 3)) & ",") = 0 Then
thisRow.Slicer2Col_List = thisRow.Slicer2Col_List & Trim(thisAreaRange(r, 3)) & ","
End If
End If
Next
Next
'Create arrays of selected items in Slicer1 and Slicer2
numSlicer1SelectedItems = 0
For Each slItem In Slicer1SC.SlicerItems
If slItem.Selected Then
numSlicer1SelectedItems = numSlicer1SelectedItems + 1
ReDim Preserve Slicer1SelectedItems(1 To numSlicer1SelectedItems)
Slicer1SelectedItems(numSlicer1SelectedItems) = slItem.Value
End If
Next
numSlicer2SelectedItems = 0
For Each slItem In Slicer2SC.SlicerItems
If slItem.Selected Then
numSlicer2SelectedItems = numSlicer2SelectedItems + 1
ReDim Preserve Slicer2SelectedItems(1 To numSlicer2SelectedItems)
Slicer2SelectedItems(numSlicer2SelectedItems) = slItem.Value
End If
Next
'Check if each row should be hidden, depending on whether selected items in Slicer1 and Slicer2 are found in Slicer1Col_List and Slicer2Col_List
'for each visible name
Application.ScreenUpdating = False
For Each areaRange In visibleAreas
thisAreaRange = areaRange.Value
For r = 1 To UBound(thisAreaRange)
Set thisRow = VisibleNamesDict(thisAreaRange(r, 1))
FoundSlicer1Count = 0
For i = 1 To UBound(Slicer1SelectedItems)
If InStr("," & thisRow.Slicer1Col_List, "," & Slicer1SelectedItems(i) & ",") Then FoundSlicer1Count = FoundSlicer1Count + 1
Next
FoundSlicer2Count = 0
For i = 1 To UBound(Slicer2SelectedItems)
If InStr("," & thisRow.Slicer2Col_List, "," & Slicer2SelectedItems(i) & ",") Then FoundSlicer2Count = FoundSlicer2Count + 1
Next
If FoundSlicer1Count <> numSlicer1SelectedItems Or FoundSlicer2Count <> numSlicer2SelectedItems Then
'Hide row because Slicer1Col_List for this Col A value is not the same as the selected Slicer1 slicer items,
'or the Slicer2Col_List for this Col A value is not the same as the selected Slicer2 slicer items
areaRange.Item(r, 1).EntireRow.Hidden = True
End If
Next
DoEvents
Next
Application.ScreenUpdating = True
End If
Set visibleAreas = Nothing
On Error Resume Next
Set visibleAreas = table.DataBodyRange.SpecialCells(xlCellTypeVisible).Areas
On Error GoTo 0
numRows = 0
If Not visibleAreas Is Nothing Then
'Count number of visible rows
For Each areaRange In visibleAreas
numRows = numRows + areaRange.Rows.Count
Next
End If
Application.StatusBar = numRows & " matching rows found in " & table.DataBodyRange.Rows.Count & " rows"
End Sub