Excel VBA refresh pivot table

byte00

New Member
Joined
Jul 8, 2015
Messages
13
Office Version
  1. 2010
Platform
  1. Windows
Hi,

I am working on a macro that is supposed to refresh a pivottable in a protected worksheet.
I put my macro bellow:

Code:
Sub refresh()
'
' refresh Macro
'

'
    ActiveSheet.Unprotect Password:="password"
    Rows("4:6").Select
    Selection.EntireRow.Hidden = False

    [B]ActiveSheet.PivotTables("orderseur").PivotCache.refresh[/B]

    Rows("5:5").Select
    Selection.EntireRow.Hidden = True
    ActiveSheet.protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFiltering:=True, AllowUsingPivotTables:=True
   
    
End Sub

This code works until the refresh row, that raises an error 1004 Application-defined or object-defined error and asks me to end or debug.
I do not understand why and how to solve the error.

Thank you.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Problem solved. Code changed to this

Code:
Sub refresh()
'
' refresh Macro
'

'
    ActiveSheet.Unprotect Password:="password"
    Rows("4:6").Select
    Selection.EntireRow.Hidden = False

    ActiveSheet.PivotTables("orderseur").RefreshTable

    MsgBox "<code>Finished refreshing data</code>"
    
    DoEvents

    Rows("5:5").Select
    Selection.EntireRow.Hidden = True
    ActiveSheet.protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFiltering:=True, AllowUsingPivotTables:=True
   
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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