Retrieving pivot items for pivot table value cells without using PivotCell

C

Casey

In ExcelXP and later, there is a PivotCell object (with RowItems and
ColumnItems properties) that can be used to easily retrieve the pivot items
for any value cell in a PivotTable, including subtotal and grand total
cells.

However, in Excel2000, there is no PivotCell. To find pivot items
associated with a single value cell, I iterate all the pivot table's pivot
fields and their pivot items, looking for the pivot items whose data range
contains the cell in question. See code below. This works fine for regular
value cells, however, it does not work for subtotal and grand total cells
because they are not treated as part of a pivot item's data range.

The question: Is there any way to reliably get all the pivot items that
make-up subtotal and grand total cells in a pivot table (in Excel2000)?

Thanks in advance.
Casey

Sub FindPivotItemsForActiveCellWithoutUsingPivotCell()

Dim pt As PivotTable
Dim itemDataRange As Range
Dim pivotItemsForActiveCell As String

Set pt = ActiveCell.PivotTable

For Each pf In pt.PivotFields
For Each pItem In pf.PivotItems
If Not Application.Intersect(pItem.DataRange, ActiveCell) Is
Nothing Then
pivotItemsForActiveCell = pivotItemsForActiveCell &
pItem.Name & vbCrLf
End If
Next
Next
MsgBox pivotItemsForActiveCell

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top