且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel VBA循环筛选的枢轴项目

更新时间:2022-12-12 14:28:50

您已经发现有关PivotItems可见性的信息:

As you already found out about the visibility of PivotItems:

如果您过滤PivotTable,则RowFieldsColumnFields中的某些PivotItems在光学上是可见的,
,但VBA仍将每个 PivotField.PivotItem作为Visible返回.

If you filter a PivotTable, then some of the PivotItems of your RowFields or ColumnFields are optically visible or not,
but VBA still returns each PivotField.PivotItem as Visible.

PivotField.VisibleItems.Count始终保持最大值.

每个枢轴的PivotLine.PivotLineCell.PivotItem可以解决剩余的真正"可见的PivotItems.

The remaining "really" visible PivotItems can be addressed by PivotLine.PivotLineCell.PivotItem of each pivot-axis.

LineType在规则行,空白行,小计行和宏计行之间进行区分.

LineType distingushes between regular, blank, subtotal and grandtotal lines.

showstring = ""
For rowItemNo = 1 To pvt.PivotRowAxis.PivotLines.Count
    If pvt.PivotRowAxis.PivotLines(rowItemNo).LineType = xlPivotLineRegular Then
        For colItemNo = 1 To pvt.PivotColumnAxis.PivotLines.Count
            If pvt.PivotColumnAxis.PivotLines(colItemNo).LineType = xlPivotLineRegular Then
                showstring = showstring & _
                pvt.PivotRowAxis.PivotLines(rowItemNo).PivotLineCells(1).PivotItem.Name & ":" & _
                pvt.PivotColumnAxis.PivotLines(colItemNo).PivotLineCells(1).PivotItem.Name & _
                " = " & pvt.DataBodyRange.Cells(rowItemNo, colItemNo).Value & vbCrLf
            End If
        Next colItemNo
    End If
Next rowItemNo
MsgBox showstring

如果您有多个列或行字段,则PivotLineCells()可以区分它们.

If you have more than one column or row field, then PivotLineCells() can distinguish them.

...,或者在每个PivotFieldDataRange上循环以在简单数据透视表的可见单元格中捕获项目:

... or you loop over the DataRange of each PivotField to catch the items in visible cells of a simple pivot table:

For rowFldNo = 1 To pvt.RowFields.Count
    For colFldNo = 1 To pvt.ColumnFields.Count
        For rowItemNo = 1 To pvt.RowFields(rowFldNo).DataRange.Rows.Count
            For colItemNo = 1 To pvt.ColumnFields(colFldNo).DataRange.Columns.Count
                showstring = showstring & ...
            Next colItemNo
        Next rowItemNo
    Next colFldNo
Next rowFldNo