Pivot table looping problem

flipcube

Board Regular
Joined
Aug 29, 2011
Messages
134
Hi All,

I have a workbook where I create several Pivot Tables (one tab for each). Each Pivot Table will be updated every fiscal month and each will be updated with the same information. I am having trouble coding a For Each Next Loop to update each pivot table. I used the macro recorder to retrieve the below code and tailored it as needed...works fine for the activesheet. How do I code the For Each Next loop to activate each sheet and update the Pivot Table...below is for January...I have similar code for each month.

Private Sub CommandButton9_Click()
'This Subroutine updates the Pivot Tables based on current fiscal month
Dim FiscalMonth As String
Dim Today As Date
Dim Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec As String
Dim lastyear As String
Dim currentyear As String
Dim nextyear As String

lastyear = Year(Date) - 1
currentyear = Year(Date)
nextyear = Year(Date) + 1

Today = Sheet1.Cells(1, 12)
If Today < Sheet2.Cells(4, 2) Then
FiscalMonth = "January"
'Removes previous year October
Oct = "Oct" & " " & lastyear
ActiveSheet.PivotTables("PivotTable1").PivotFields(Oct).Orientation = _xlHidden
'Adds fiscal year October
Oct = Format("Oct" & " " & "-" & " " & currentyear, "mmm-yy")
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields(Oct), "Oct" & " " & Year(Date), xlCount
Color_change
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Dec" & " " & Year(Date))
.Function = xlSum
.NumberFormat = "#,##0"
End With


Thanks in advance
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I solved the problem...here is my solution in case someone else has the same issue.

Private Sub CommandButton9_Click()
'This Subroutine updates the Pivot Tables based on current fiscal month
Dim FiscalMonth As String
Dim Today As Date
Dim month As String
Dim Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec As String
Dim lastyear As String
Dim currentyear As String
Dim nextyear As String
Dim sh As Worksheet
Dim pt As PivotTable

lastyear = Year(Date) - 1
currentyear = Year(Date)
nextyear = Year(Date) + 1

Today = Sheet45.Cells(1, 12)

If Today < Sheet46.Cells(4, 2) Then
FiscalMonth = "January"
For Each sh In Worksheets
For Each pt In sh.PivotTables
If sh.Name <> "RAW" Or sh.Name <> "Fiscal_Calendar" Then
sh.Activate
'Removes previous year October
Oct = "Oct" & " " & lastyear
sh.PivotTables("PivotTable1").PivotFields(Oct).Orientation = _
xlHidden
'Adds fiscal year October
Oct = Format("Oct" & " " & "-" & " " & currentyear, "mmm-yy")
sh.PivotTables("PivotTable1").AddDataField sh.PivotTables( _
"PivotTable1").PivotFields(Oct), "Oct" & " " & Year(Date), xlCount
Color_change
With sh.PivotTables("PivotTable1").PivotFields("Dec" & " " & Year(Date))
.Function = xlSum
.NumberFormat = "#,##0"
End With
End If
Next pt
Next sh

End sub
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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