Refresh Pivot table

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
218
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

I am using the code below to refresh a pivot table, which is in another worksheet but in the same workbook, not sure what I am missing because it only works if I run it within Sales worksheet. I thought that using the whole object's hierarchy that would work.

VBA Code:
Sub RefreshSales()

    ActiveWorkbook.Sheets("Sales").PivotTables(1).PivotCache.Refresh
    ActiveWorkbook.Sheets("Sales").PivotTables(1).RefreshTable
    MsgBox "Refresh completed", vbInformation
  
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
@Guinaba - It works fine for me.

Do you have it in a standard module (not in a worksheet module) ?
How are you running the code ?
Is there only 1 pivot table on the Sales Tab ?
 
Upvote 0
@Guinaba - It works fine for me.

Do you have it in a standard module (not in a worksheet module) ?
How are you running the code ?
Is there only 1 pivot table on the Sales Tab ?
Hi @Alex Blakenburg,

I am using it in a standard module (below pic) and I run the code calling this Sub from a button in another worksheet called "Summary" and yes there is only one pivot table in there. I've tried adding the name as well "ptSales" but didn't work. Really weird!




1632220133867.png
 
Upvote 0
Hi @Alex Blakenburg,

I am using it in a standard module (below pic) and I run the code calling this Sub from a button in another worksheet called "Summary" and yes there is only one pivot table in there. I've tried adding the name as well "ptSales" but didn't work. Really weird!
Hi @Alex Blakenburg, just realized that if I run the sub from other sheet whithout using the button (code below), it works. So not sure why the call method doesn't :(

VBA Code:
Private Sub CommandButton2_Click()
   Call RefreshSales
End Sub
 
Upvote 0
I was about to address that very point.

Can you put a msgbox as the first line of the code and then hit the run button.
(or a debug.print "Executing RefreshSales" OR a breakpoint)

My guess is that your button is not calling the code.
 
Upvote 0
Did you by any chance rename the button after you added the code ?
Either go into the buttons properties or if you have only a few objects select the button and hit Alt+F10 to view the object property pane in Excel.
 
Upvote 0
Try this:

VBA Code:
Sub RefreshSales()
    With ActiveWorkbook.Sheets("Sales").PivotTables(1)
      .PivotCache.MissingItemsLimit = xlMissingItemsNone
      .PivotCache.Refresh
      .RefreshTable
    End With
    MsgBox "Refresh completed", vbInformation
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,841
Members
449,193
Latest member
MikeVol

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