View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Crypto Crypto is offline
external usenet poster
 
Posts: 3
Default PivotTable.columnfields(index) vs Pivotcell.columnitem(index)

I have a pivot table that looks like the following:
(Rowfields : Country, State
Columnfields: Company, Department)

Dell
Sales | Marketing
USA | CA 8 10
| FL 2 3

As I'm trying to automate the process of extracting data from this
table using GetPivotData method, I ran into a problem. I was puzzled
to find that PT.ColumnFields(1) returns "Department" not "Company,"
yet pcell.columnitems(1) returns "Dell", not "Sales." In other words,
columnitems use the index the items in the order they appear, but
columnfields do not. I think what's happening here is that
Columnfields index the items in the order added (to the pivottable),
regardless of its actual location it is placed relative to the other
column fields.
Apparently, this causes errors with GetPivotData because there is no
Department called Dell.

Set PT = Worksheets("test").PivotTables(1)
Set pcell = Activecell.PivotCell

PT.GetPivotData(pcell.DataField, PT.RowFields(1), pcell.RowItems(1), _
PT.RowFields(2),
pcell.RowItems(2), _
PT.ColumnFields(1),
pcell.ColumnItems(1), _
PT.ColumnFields(2),
pcell.ColumnItems(2))

With that said, is there a way to get the field name based on the
actual location?

Thanks for your help in advance.