Excel:根据表格行创build切片器

是否有可能创build一个基于表头标题行,而不是只是一个标题列,如在下面的模型? 或者可以有人推荐一个更好的方式来组织这个双轴数据matrix?

我希望这个表格可能有数百个条目,而且只需简单地比较一小部分材料将是有用的。

双轴数据矩阵与切片机的头列和行

这是可能的一些VBA代码,请参阅隐藏和取消隐藏(filter)列与切片器或filter下拉我应该说我没有testing过自己,但代码是直截了当,不言而喻。

您需要修改代码,但基本设置是:

  1. 包含Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)macros。

…哪个叫…

  1. 一个较长的macros遍历列并隐藏那些不符合您的标准的macros。

他们给出的代码并不是特别复杂(见下文),但我认为对VBA新手来说,这将是具有挑战性的,但并非不可能。

 Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) 'Run the filter columns macro when the pivot table or slicer is changed Select Case Target.Name Case "PivotTable1" Call m_FilterCol.Filter_Columns("rngQuarter", "Report", "Report", "PivotTable1", "Quarter") End Select End Sub 

这里是Filter_Columns的macros代码。

 Sub Filter_Columns(sHeaderRange As String, _ sReportSheet As String, _ sPivotSheet As String, _ sPivotName As String, _ sPivotField As String _ ) Dim c As Range Dim rCol As Range Dim pi As PivotItem 'Unhide all columns Worksheets(sReportSheet).Range(sHeaderRange).EntireColumn.Hidden = False 'Loop through each cell in the header range and compare to the selected filter item(s). 'Hide columns that are not selected/filtered out. For Each c In Worksheets(sReportSheet).Range(sHeaderRange).Cells 'Check if the pivotitem exists With Worksheets(sPivotSheet).PivotTables(sPivotName).PivotFields(sPivotField) On Error Resume Next Set pi = .PivotItems(c.Value) On Error GoTo 0 End With 'If the pivotitem exists then check if it is visible (filtered) If Not pi Is Nothing Then If pi.Visible = False Then 'Add excluded items to the range to be hidden If rCol Is Nothing Then Set rCol = c Else Set rCol = Union(rCol, c) End If End If End If 'Reset the pivotitem Set pi = Nothing Next c 'Hide the columns of the range of excluded pivot items If Not rCol Is Nothing Then rCol.EntireColumn.Hidden = True End If End Sub