简体   繁体   中英

Error when accessing pivot table using VBA

I have a PivotTable in Excel that looks like this:

Account                e-mail   Sum of Price    Sum of Billing Price
Customer Z             blah@gmail.com       30            33.75
Customer Z Total                            30            33.75
Customer Y             blah@gmail.com       10            10.5
Customer Y Total                            10            10.5
Grand Total                                 40              44.25

I have the following VBA Code:

For j = 2 To pt.RowFields(1).PivotItems.Count
    Sheets("Configuration").Range("j2").Value = Sheets("Configuration").Range("j2").Value + 1

    Client = pt.RowFields("Account").PivotItems(j)
    sum = pt.GetPivotData("Sum Of Billing Price", "Account", Client)
    net = pt.GetPivotData("Sum Of Price", "Account", Client)
    email = pt.RowFields("e-mail").PivotItems(j)
    ...

When I run the code, I receive the error: Unable To Get PivotItems Property Of PivotField Class when assigning the value to e-mail. Assigning the value to Client works as expected. I have also tried using index numbers, with same result.

It looks like you don't have emails in every row. You may want to put a conditional statement in to deal with this.

This problem turned out to be that VBA could not handle duplicate e-mail addresses. Change one of the e-mail addresses by one character and it works fine.

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