1

我正在使用下面的代码循环遍历一个数据透视字段以获取可见的数据透视项目。但是当数据透视字段在行标签pivotitem.count中时给出 0 当我将此数据透视字段移动到列标签时,代码工作正常。但我需要这个字段留在行标签上。

我的问题有什么解决方法吗?

Dim pt As PivotTable
Dim pf As PivotField
Dim pvtitem As PivotItem

Set nwSheet = Worksheets.Add
nwSheet.Activate
rw = 0

Set pt = Sheets("Reasons").PivotTables("PivotFields")
Set pf = pt.PivotFields("[Characteristics].[Reason].[Reason]")

With pf
    For i = 0 To .PivotItems.Count
        rw = rw + 1
        nwSheet.Cells(rw, 1).Value = .PivotItems.Count
    Next i
End With
4

1 回答 1

1

显式地对 RowFields 进行迭代可以获得对 Row Fields 中可见 Pivot Items 的句柄。请看看这是否符合目的:

    Set pt = Sheets("Reasons").PivotTables("PivotFields")
    Dim pf As PivotField
    For Each pf In pt.RowFields
          MsgBox pf.Name & " : " & pf.VisibleItems.Count
    Next

要迭代报告过滤器:

Dim pt As PivotTable
Dim pFilter As PivotFilter
Dim pFilters As PivotFilters
Dim pF As PivotField

Set pt = Sheets("Reasons").PivotTables("PivotFields")
'Set the first RowField as the PivotField
Set pF = pt.RowFields(1)

'Remove previous filters
pF.ClearAllFilters

'Assuming we have a RowField as 'Quarter' and DataField as 'Sum of Sale',
'we can apply a new 'ValueIsLessThan' filter for a Sum of Sale value of 12000

pt.PivotFields("Quarter").PivotFilters.Add Type:=xlValueIsLessThan, _
DataField:=pt.PivotFields("Sum of Sale"), Value1:=12000

Set pFilters = pt.PivotFields("Quarter").PivotFilters

For Each pFilter In pFilters
  MsgBox "Filter Applied On: " & pFilter.PivotField.Name & " -- Filtered values for less than : " & pFilter.Value1
Next
于 2016-08-02T04:05:53.390 回答