如何调整数据透视表过滤器

问题描述 投票:1回答:1

我正在尝试创建一个宏,将一些数据从数据透视表拉入集合变量。我想要做的是每个月收集每篇文章的销售预测和洞察力一年(图片如下)。我遇到的问题是,我不知道如何收集信息而不显示在数据透视表中,我不知道如何让宏调整列过滤器以满足我的要求。

enter image description here

我无法访问此数据透视表的来源,我不愿让用户手动调整表的列过滤器。

为了澄清,列过滤器按年 - >季度 - >月 - >周。

编辑:

我想要完成的工作始于一个计划工作簿,其中保留了相关的文章编号。主计划程序具有不同的工作簿,其输入值(上面的数据透视表)基于计算。这个输入工作簿只有数据透视表,我不知道它在哪里提取数据;这就是Master Scheduler手动查找相关数据的地方。

我的思考过程是从调度工作簿中提取相关文章,将所有文章拉一整年,仅排序相关文章,并将排序后的信息放入Schedule工作簿中的新位置。

我遇到的问题是列过滤器可能没有显示所有相关数据,我不知道如何让VBA设置数据透视表的过滤器以防止这种情况发生。我发现的解决方案将F9单元格“Year”更改为实际年份值,这不是我想要做的。

此外,当您手动打开列过滤器以选择哪一年时,有四个季度(或季节),其中所有四个必须在框中显示所有十二个月的可用标记。

希望这澄清了我的问题。

excel vba excel-vba pivot-table
1个回答
0
投票

经过一些研究,我发现这个网站回答了我的问题:Expand and Collapse Entire Pivot Table Fields – VBA Macro。我也了解了pt.ClearAllFilters命令。

如果该网站将来不可用,则代码也会在下面列出。

扩大:

Sub Expand_Entire_RowField()
    'Expand the lowest position field in the Rows area
    'that is currently expanded (showing details)

    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim iFieldCount As Long
    Dim iPosition As Long

      'Create reference to 1st pivot table on sheet
      'Can be changed to reference a specific sheet or pivot table.
      Set pt = ActiveSheet.PivotTables(1)

      'Count fields in Rows area minus 1 (last field can't be expanded)
      iFieldCount = pt.RowFields.Count - 1

      'Loop by position of field
      For iPosition = 1 To iFieldCount
        'Loop fields in Rows area
        For Each pf In pt.RowFields
          'If position matches first loop variable then
          If pf.Position = iPosition Then
            'Loop each pivot item
            For Each pi In pf.PivotItems
              'If pivot item is collapsed then
              If pi.ShowDetail = False Then
                'Expand entire field
                pf.ShowDetail = True
                'Exit the macro
                Exit Sub
              End If
            Next pi
          End If
        Next pf
      'If the Exit Sub line is not hit then the
      'loop will continue to the next field position
      Next iPosition

    End Sub

坍方:

Sub Collapse_Entire_RowField()
'Collapse the lowest position field in the Rows area
'that is currently expanded (showing details)

Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim iFieldCount As Long
Dim iPosition As Long

  'Create reference to 1st pivot table on sheet
  'Can be changed to reference a specific sheet or pivot table.
  Set pt = ActiveSheet.PivotTables(1)

  'Count fields in Rows area minus 1 (last field can't be expanded)
  iFieldCount = pt.RowFields.Count - 1

  'Loop backwards by position of field (step -1)
  For iPosition = iFieldCount To 1 Step -1
    'Loop fields in Rows area
    For Each pf In pt.RowFields
      'If position matches first loop variable then
      If pf.Position = iPosition Then
        'Loop each pivot item
        For Each pi In pf.PivotItems
          'If pivot item is expanded then
          If pi.ShowDetail = True Then
            'Collapse entire field
            pf.ShowDetail = False
            'Exit the macro
            Exit Sub
          End If
        Next pi
      End If
    Next pf
  'If the Exit Sub line is not hit then the
  'loop will continue to the next field position
  Next iPosition

End Sub

编辑:解决方案特定

Dim pt As PivotTable
Dim pf As PivotField

Set pt = ws.PivotTables(1) 

'Setting all of the column filters to desired setup
For Each pf In pt.ColumnFields
    pf.Hidden = False
    pf.ClearAllFilters

    'Drill Down until Months are opened, then exit the loop
    If UBound(Split(pf.Name, "Month")) > 0 Then
        pf.DrilledDown = False  '<-- Ensuring nothing beneath "Months" is 
        Exit For
    Else
        pf.DrilledDown = True
    End If
Next
© www.soinside.com 2019 - 2024. All rights reserved.