Cycle through Pivot Table values -

Dennis_S

New Member
Joined
Mar 12, 2018
Messages
2
Hello,
I'm trying to cycle trough values in very simple Pivot Table. Two rows and one Sum value.
Now I see the very strange shift during the cycle - please see the screenshot.

zAh4Q.png


Yellow outlined cells are shifted - with the incorrect "Month". Red outlined cells are the duplicates - they are the copy of green range. And the green ones are correct till the end of output. The code is pasted below. Any combinations of pi / pi2 and Caption / Value in the cycle are giving the similar incorrect result but I can not obtain the correct one.
Code:
Sub jayd12()
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pf2 As PivotField
    Dim pi As PivotItem
    Dim pi2 As PivotItem
    
    Set pt = Worksheets("CaissePivot").PivotTables("PivotCaisse")
    Set pf = pt.PivotFields("Year")
    Set pf2 = pt.PivotFields("Month")
    For Each pi In pf.PivotItems
        For Each pi2 In pf2.PivotItems
            If pi.DataRange(pi2.Position, 1).Value <> "" Then _
            Debug.Print pi.Value & "  " & pi2.Caption & "  " & pi.DataRange(pi2.Position, 1).Value
        Next pi2
    Next pi
End Sub

I understand that I'm wrong in using members and collection items.
Could anyone explain how to cycle through the headers and values in the Pivot?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Welcome to Mr Excel

Maybe...

Code:
Sub jayd12()
    Dim pt As PivotTable
    Dim rCell As Range
    
    Set pt = Worksheets("CaissePivot").PivotTables("PivotCaisse")
    
    For Each rCell In pt.PivotFields("Year").DataRange.Cells
        Debug.Print rCell & " " & rCell.Offset(, 1) & " " & rCell.Offset(, 2)
    Next rCell
End Sub

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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