Loop broken

jack3d

New Member
Joined
Jul 18, 2011
Messages
4
I have written a program to take data from a pivot table and put it into another spreadsheet to be used for static graphs. The goal is for it to loop by month, copy the data for each month, and then move on to the next one. However, the loop only works for Month 1. When it gets to months 2-8 the Selection.Copy function seems to stop working, even though the rest of the code runs.

Help please.


Code:
For w = 1 To Month(Date)
    
    For pl = 1 To Month(Date)
    Sheets("PivotTable").PivotTables("PivotData").PivotFields("Month").PivotItems(pl).Visible = False
    Next pl
    
    For i = 1 To Month(Date)
    If i = w Then
    Sheets("PivotTable").PivotTables("PivotData").PivotFields("Month").PivotItems(i).Visible = True
    End If
    
    Sheets("PivotTable").PivotTables("PivotData").PivotFields("Month").PivotItems("").Visible = False
    Sheets("PivotTable").PivotTables("PivotData").PivotFields("Month").PivotItems("(blank)").Visible = False
        

    With Sheets("PivotTable").PivotTables("PivotData").PivotFields("Month")
        .Orientation = xlPageField
        .Position = 2
    End With

    Sheets("PivotTable").PivotTables("PivotData").PivotSelect "", xlDataAndLabels, True
    Selection.Copy
    Sheets("PivotData").Select
    Range("A1").Select
    ActiveCell.Offset(0, 6 * (w - 1)).Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    

    With Sheets("PivotTable").PivotTables("PivotData").PivotFields("Month")
        .Orientation = xlRowField
        .Position = 1
    End With

Next i
 Next w
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi,

Try the code below. It sets .EnableMultiplePageItems = False which allows you to show just one PageField at a time without having to hide all the others.

This might not do exactly what you were trying to do, but it might point you in the direction of a simpler approach.
Code:
Sub LoopThroughPTMonths()
    Dim w As Long
    Application.ScreenUpdating = False
    With Sheets("PivotTable").PivotTables("PivotData").PivotFields("Month")
        .Orientation = xlPageField
        .EnableMultiplePageItems = False
        .Position = 2
        For w = 1 To Month(Date)
            .CurrentPage = w
            Sheets("PivotTable").PivotTables("PivotData").TableRange2.Copy
            Sheets("PivotData").Range("A1").Offset(0, 6 * (w - 1)) _
                .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
        Next w
        .Orientation = xlRowField
        .Position = 1
    End With
End Sub
 
Upvote 0
Hi,

Try the code below. It sets .EnableMultiplePageItems = False which allows you to show just one PageField at a time without having to hide all the others.

This might not do exactly what you were trying to do, but it might point you in the direction of a simpler approach.
Code:
Sub LoopThroughPTMonths()
    Dim w As Long
    Application.ScreenUpdating = False
    With Sheets("PivotTable").PivotTables("PivotData").PivotFields("Month")
        .Orientation = xlPageField
        .EnableMultiplePageItems = False
        .Position = 2
        For w = 1 To Month(Date)
            .CurrentPage = w
            Sheets("PivotTable").PivotTables("PivotData").TableRange2.Copy
            Sheets("PivotData").Range("A1").Offset(0, 6 * (w - 1)) _
                .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
                SkipBlanks:=False, Transpose:=False
        Next w
        .Orientation = xlRowField
        .Position = 1
    End With
End Sub


worked beautifully, thank you
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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