ピボットテーブルのスライサーの選択をキャプチャできるUDFをオンラインで見つけましたが、ピボットテーブルがExcelテーブルに基づいている場合にのみ機能します。 PowerPivotピボットテーブル(私の場合のような)の場合は機能しません。PowerPivotピボットテーブルスライサー選択
このポストPowerPivot Slicer selection based on cell value using VBAでは、PowerPivotピボットテーブルを使用する場合、SlicerCacheではなくSlicerCacheLevelオブジェクトにアクセスする必要があることが説明されています。
誰かがPowerPivot PivotTablesスライサーで動作するように次のUDFを変更することができれば、それは素晴らしいことです。多くのExcelユーザーがあなたの助けを祈ってくれます。 最初の1:「http://www.jkp-ads.com/Articles/slicers05.asp
Public Function GetSelectedSlicerItems(SlicerName As String) As String
Dim oSc As SlicerCache
Dim oSi As SlicerItem
Dim lCt As Long
On Error Resume Next
Application.Volatile
Set oSc = ThisWorkbook.SlicerCaches(SlicerName)
If Not oSc Is Nothing Then
For Each oSi In oSc.SlicerItems
If oSi.Selected Then
GetSelectedSlicerItems = GetSelectedSlicerItems & oSi.Name & ", "
lCt = lCt + 1
ElseIf oSi.HasData = False Then
lCt = lCt + 1
End If
Next
If Len(GetSelectedSlicerItems) > 0 Then
If lCt = oSc.SlicerItems.Count Then
GetSelectedSlicerItems = "All"
Else
GetSelectedSlicerItems = Left(GetSelectedSlicerItems, Len(GetSelectedSlicerItems) - 2)
End If
Else
GetSelectedSlicerItems = "No items selected"
End If
Else
GetSelectedSlicerItems = "No slicer with name '" & SlicerName & "' was found"
End If
End Function
Public Function FblSlicerSelections(Slicer_Name As String, Optional Delimiter As Variant, Optional Wrap_Length As Variant)
' Type Variant must be used for Optional Parameters for the IsMissing function to work below.
Dim i, r, s As Integer: r = 1: s = 0 ' i = slicer Item, r = Rows in output, s = count of Selected items
FblSlicerSelections = ""
If IsMissing(Delimiter) Then Delimiter = " "
If IsMissing(Wrap_Length) Then Wrap_Length = 40
With ActiveWorkbook.SlicerCaches(Slicer_Name)
For i = 1 To .SlicerItems.Count
If .SlicerItems(i).Selected Then
s = s + 1 ' Selected count increment
If .SlicerItems(i).HasData Then
If Len(FblSlicerSelections) > r * Wrap_Length Then
FblSlicerSelections = FblSlicerSelections & vbCr & " "
r = r + 1.2 ' Modify multiplier used to determine when to wrap output (via carriage return)
End If
FblSlicerSelections = FblSlicerSelections & .SlicerItems(i).Value & Delimiter
End If
End If
Next i
If s = .SlicerItems.Count Then FblSlicerSelections = "All" & Delimiter ' Selected count = SlicersItems.Count
End With
FblSlicerSelections = Left(FblSlicerSelections, Len(FblSlicerSelections) - Len(Delimiter)) ' remove extra delimiter
End Function