2016-08-26 8 views
0

ピボットテーブルのスライサーの選択をキャプチャできる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 

秒1:https://social.msdn.microsoft.com/Forums/office/en-US/d7893d81-938c-46d6-9b4c-7cd1b0b4fbf4/retrieve-the-value-selected-in-a-slicer?forum=exceldev

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 

答えて

0

さて、以上のパニック

私はここ2つのUDFとそのソースを掲載します。私は両方の機能を外部ソース(例えばPowerPivot)のスライサーで動作させるように変更しました。誰かがこれらのUDFの恩恵を受けることを願っています。

公開機能GetSelectedSlicerItems 'http://www.jkp-ads.com/Articles/slicers05.asp 薄暗いOSC SlicerCacheLevel' としてSlicerItem 薄暗いLCTは、エラー時に 限り再開したようSlicerCache 薄暗いOSI次 Application.Volatile

Set oSc = ThisWorkbook.SlicerCaches(SlicerName).SlicerCacheLevels(1) 

If Not oSc Is Nothing Then 
    For Each oSi In oSc.SlicerItems 
     If oSi.Selected Then 
      GetSelectedSlicerItems = GetSelectedSlicerItems & oSi.Caption & ", " 'Initial code: oSi.Caption // There are 3 "choices": .Caption .Name .Value 
      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 

文字列 として(文字列としてSlicerName)そして、第2のUDF:

Public Function FblSlicerSelections(Slicer_Name As String, Optional Delimiter As Variant, Optional Wrap_Length As Variant) 
Application.Volatile 
'https://social.msdn.microsoft.com/Forums/office/en-US/d7893d81-938c-46d6-9b4c-7cd1b0b4fbf4/retrieve-the-value-selected-in-a-slicer?forum=exceldev 
' 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).SlicerCacheLevels(1) 

    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 
関連する問題