Error when displaying values for pivot items.

cool1_boy1

New Member
Joined
Mar 19, 2013
Messages
21
Let's say I have a Pivot Table that looks like this but it has over 50 rows:

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Letter[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]D[/TD]
[TD]8[/TD]
[/TR]
</tbody>[/TABLE]

In the table above the Letter B has a total of 0 hours so it is filtered out; therefore, it isn't displayed.

I want to create a loop that displays the number of Hours for each Letter that is in the table. When it gets to Letter B, I get an error. What's the workaround?
Here's my code:

Code:
For Each pt_item In pt.PivotFields("Letter").PivotItems     
     MsgBox pt.PivotFields("Letter").PivotItems(pt_item.Value).DataRange.Value
Next

Thanks!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
For anyone looking for the solution, here ya go:


Code:
Sub PivotTest() 
    Dim pt As PivotTable 
    Dim c As Range 
     
     
    Set pt = ActiveSheet.PivotTables(1) 
     
     
    For Each c In pt.PivotFields("Letter").DataRange 
        MsgBox pt.PivotFields("Letter").PivotItems(c.Value).DataRange.Value 
    Next 
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top