Updating a Pivot Table with VBA

James Wiseman

New Member
Joined
Dec 2, 2005
Messages
1
Hi all,

I've written a vba code that works, but it takes some time and I'm looking for tips to improve the efficiency. There are two separate loops that share a lot of similarities but whenever I try to combine them I either get an error, or the code fails to update the table.

Any help would be greatly appreciated, James.
VBA code is:

Sub Benefit_Update()
Dim wsBP As Worksheet
Dim wsCC As Worksheet
Dim wsOS As Worksheet
Dim pt As PivotTable
Dim ws As Worksheet
Dim rngItems As Range
Dim c As Range
Dim i As Integer
Dim pf As PivotField
Dim pi As PivotItem
Set pt = Sheets("Benefit Pivot").PivotTables(1)
Set wsBP = Sheets("Benefit Pivot")
Set wsCC = Sheets("Country-Card")
Set wsOS = Sheets("Offsets")
Set SelectCountry = wsOS.Range("rTerritory")
Set SelectCard = wsOS.Range("rCards")

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
For Each ws In Sheets(Array(wsBP.Name))

For Each pt In ws.PivotTables
Set pf = pt.PivotFields("Country")
On Error Resume Next
With pf
.CurrentPage = "(All)"
.AutoSort xlManual, .SourceName
'show all pivot items
For Each pi In pf.PivotItems
pi.Visible = True
Next pi
For Each c In SelectCountry
If UCase(c.Offset(0, 1)) = "X" Then
.PivotItems(c.Value).Visible = False
End If
Next c
.AutoSort xlAscending, .SourceName
End With
Next pt

For Each pt In ws.PivotTables
Set pf = pt.PivotFields("Card")
On Error Resume Next
With pf
.CurrentPage = "(All)"
.AutoSort xlManual, .SourceName
'show all pivot items
For Each pi In pf.PivotItems
pi.Visible = True
Next pi
For Each c In SelectCard
If UCase(c.Offset(0, 1)) = "X" Then
.PivotItems(c.Value).Visible = False
End If
Next c
.AutoSort xlAscending, .SourceName
End With
Next pt
Next ws

Sheets("Benefit Details").Select

Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,217,437
Messages
6,136,615
Members
450,022
Latest member
Joel1122331

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