VBA Pivot Code Erroring Out...quick help?

nick612hayden

New Member
Joined
May 15, 2012
Messages
33
Can you direct me as to why?

Would be much appreciated!
Nick

Sub ALL()
Dim wBook As Workbook
Dim wSheet As Worksheet
Dim Pwd As String

Pwd = "password"

Application.ScreenUpdating = False

For Each wBook In Workbooks

'Unprotect all sheets at once
For Each wSheet In wBook.Worksheets
wSheet.Unprotect Password:=Pwd
Next wSheet

'Refresh all pivot tables
For Each wSheet In wBook.Worksheets
Sheets("Sheet1").Select
ActiveSheet.PivotTables("PivotTable4").RefreshTable
ActiveSheet.PivotTables("PivotTable5").RefreshTable
Sheets("Sheet2").Select
ActiveSheet.PivotTables("PivotTable1").RefreshTable
Sheets("Sheet3").Select
ActiveSheet.PivotTables("PivotTable3").RefreshTable

'Protect all sheets at once
For Each wSheet In wBook.Worksheets
wSheet.Protect Password:=Pwd, AllowFiltering:=True, _
AllowFormattingColumns:=False, AllowFormattingRows:=True, AllowUsingPivotTables:=True
Next wSheet

Next wBook

Application.ScreenUpdating = True

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Forgot to say that the error is the part where the code should be updating each pivot. It works if I manually click RefreshAll in the ribbon, but the macro wBook.RefreshAll makes it that when I try to collapse/expand field, it returns Pivot Table Saved without Underlying Data.

Specifically, the part where it is the 'Refresh all pivot tables. I am trying to specify each pivot name in each sheet (which is the same in all open workbooks) so that it works
 
Last edited:
Upvote 0
Hi Nick,

Does your code stop with a run-time error? or does it refresh all the data, but not save the underlying data?

Have you checked to see that the PivotTable Option is checked to save underlying data before running the macro?

Based on your response, I can provide some alternative code which updates each PivotCache in the workbook instead of each PivotTable.
 
Upvote 0
It appears to refresh all the data, but not save the underlying data; yes. The ony two things I have unchecked in the Data option is Retained Items Deleted from Data Source and Refresh Data When Opening File...don't see an option to save underlying data before running macro (Excel for Mac2011) :/

The alt code might be the best bet...when you have the moment. Thanks!
 
Upvote 0
Nick, Sorry for my confusing poor grammar. What I meant is...

Before running the macro during your testing, check to see that the PivotTable Option: "Save source data with file" is checked.

It sounds like you do have that option checked and that isn't the issue.

Looking at the code, there are some problems that might have left the pivots in a state that caused the grouping (expand/collapse) not to work correctly.

This part of the code is missing a Next statement which should have prevented it from compiling. (maybe a typo in posting the code to the forum).

Even if that were added, the code has an extra nested loop in that with each wSheet, it repeats the process of updating all 3 sheets.

Code:
For Each wSheet In wBook.Worksheets
     Sheets("Sheet1").Select
     ActiveSheet.PivotTables("PivotTable4").RefreshTable
     ActiveSheet.PivotTables("PivotTable5").RefreshTable
     Sheets("Sheet2").Select
     ActiveSheet.PivotTables("PivotTable1").RefreshTable
     Sheets("Sheet3").Select
     ActiveSheet.PivotTables("PivotTable3").RefreshTable
[COLOR="#FF0000"]Next wSheet  'missing[/COLOR]
'Protect all sheets at once
 For Each wSheet In wBook.Worksheets

Here's some alternative code. Hopefully your grouping functionality will be preserved after running it.

Code:
Sub Refresh_All_Pivots()
    Dim wBook As Workbook
    Dim wSheet As Worksheet
    Dim Pwd As String
    Dim PC As PivotCache
    

    Pwd = "password"
 
    Application.ScreenUpdating = False
 

    For Each wBook In Workbooks
         
        'Unprotect all sheets at once
        For Each wSheet In wBook.Worksheets
            wSheet.Unprotect Password:=Pwd
        Next wSheet
         
        'Refresh all pivot caches
        For Each PC In wBook.PivotCaches
            PC.Refresh
        Next PC
         
        'Protect all sheets at once
        For Each wSheet In wBook.Worksheets
            wSheet.Protect Password:=Pwd, AllowFiltering:=True, _
            AllowFormattingColumns:=False, AllowFormattingRows:=True, _
            AllowUsingPivotTables:=True
        Next wSheet
 
        
    Next wBook
     
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Jerry

Worked like a charm...glad I don't have to go through those 3steps, for each Workbook, 50 times every month :)

Thanks much!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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