简体   繁体   中英

How can I reference a Data Value for a subfield in a Pivot Table?

In the attached screenshot below, I'm trying to get the Count Of Height of "Short" in the "First Grade" Row, which would be 12.5%.

It looks like I need to use the .GetPivotData method, but I cannot for the life of me get it to work. It says I need PivotTable.GetPivotData([DataField],[Field1],[Item1]). But when I try

x = pvt.GetPivotData("Count of Height", "First Grade", "Short").Value

I get Run-time error '1004': Application-defined or object-defined error.

Sub PivotTest()

    Dim pvt As PivotTable: Set pvt = Sheets("Sheet2").PivotTables("PivotTable1")

    x = pvt.GetPivotData("Count Of Height", "First Grade", "Short").Value

End Sub

I really can't see what I'm missing. See the attached image for the table

在此处输入图片说明

Looking at the GetPivotData function, it requires at least 1 argument, the Data Field, which you have. Then for each subsequent field it needs two arguments, the field name and the item of the field.

The code you wrote is missing the field names for each item.

That said, this will work:

x = pvt.GetPivotData("Count Of Height", "Grade", "First Grade", "Height", "Short").Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM