VBA code required to amend specific pivot tables on a worksheet

Dobo_Bobo

New Member
Joined
Jan 24, 2018
Messages
31
Please can someone help me by tweaking tis code? I'm learning VBA code and I can't find a site that explains how to amend specific pivot tables on a worksheet. The code below will set all of the pivot tables on one sheet to the required 12 week range. But, I have another sheet where I only want to set the 12 week range for specific pivot tables. Thus, instead of 'For Each pt In ActiveSheet.PivotTables' I need the macro to amend PivotTable1 and PivotTable3 an ignore PivotTable2.

I still have a lot to learn about VBA so I don't know what to use instead of 'For Each pt In ActiveSheet.PivotTables'. If anyone can help me I will return to give thanks and I will sincerely appreciate your help!

Code:
Sub SetTwelveWeekRange()



Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim StartWeek As Integer
Dim FinishWeek As Integer
'Dim begintwo As Integer '*This code needs to be switched on when the 10 week range covers the change of year.
'Dim finishtwo As Integer '*This code needs to be switched on when the 10 week range covers the change of year.

On Error GoTo Err
StartWeek = Range("P4")
FinishWeek = Range("P5")
'NYStartWeek = Range("r2") '*This code needs to be switched on when the 10 week range covers the change of year.
'NYFinishWeek = Range("r1") '*This code needs to be switched on when the 10 week range covers the change of year.

For Each pt In ActiveSheet.PivotTables


Set pf = pt.PivotFields("WkNo")

    pf.EnableMultiplePageItems = True



For Each pi In pf.PivotItems
    
    pi.Visible = pi >= StartWeek And pi <= FinishWeek 'Or pi >= NYStartWeek And pi <= NYFinishWeek

Next pi
Next pt
Err:
Resume Next
 



End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
EDIT: I know one option is to set the active field 'WkNo' only on the specific pivot tables I need amending, but I need learn if there is an alternaive to 'For Each pt...'.

I've tried
Code:
Set pt = ActiveSheet.Pivotables("PivotTable1", "PivotTable3")
but even with that code every pivot table is amended.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,608
Messages
6,120,500
Members
448,968
Latest member
screechyboy79

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