在这里我们需要借助VBA以及工作表事件来完成这个操作。
第一步:创建辅助用透视表
Private Sub Worksheet_PivotTableChangeSync(ByVal Target As PivotTable)
Dim slc As SlicerCache
Set slc = ActiveWorkbook.SlicerCaches("切片器_类别")
For Each slcItem In slc.SlicerItems
If slcItem.Selected = True Then
Dim pvt As PivotTable, pvtField As PivotField
Set pvt = Sheet1.PivotTables("数据透视表1")
For Each pvtField In pvt.DataFields
pvtField.Orientation = xlHidden
Next
pvt.AddDataField pvt.PivotFields(slcItem.Caption), slcItem.Caption & " ", xlSum
End If
Next
End Sub
好了,本期教程就到这里啦,走过路过的点个关注分享一下吧,谢谢啦