Refresh Pivot same time as ODBC refresh

dibblejon

New Member
Joined
Sep 23, 2010
Messages
37
Hi

I have a spreadsheet connected to our in house system via ODBC. When I refresh sheet2(data) I need sheet1(summary) - which is a pivot table using the data from sheet2(data) to update as well.

Any help on how I could achieve would be great.

Many thanks

Jon D
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

dibblejon

New Member
Joined
Sep 23, 2010
Messages
37
Apologies I should of been clearer.

When I hit the "refresh all" button the table connected via ODBC refreshes but the pivot table does not.
 

jkpieterse

Well-known Member
Joined
Dec 3, 2007
Messages
1,155
In that case it may be better to use a bit of VBA code:

Code:
Sub RefreshAllInOrder()
    Dim oQT As QueryTable
    Dim oPc As PivotCache
    Dim oSh As Worksheet
    For Each oSh In Worksheets
        For Each oQT In oSh.QueryTables
            oQT.Refresh False
        Next
    Next
    For Each oPc In ActiveWorkbook.PivotCaches
        oPc.Refresh
    Next
End Sub
 

dibblejon

New Member
Joined
Sep 23, 2010
Messages
37

ADVERTISEMENT

Thanks - I am a complete VBA newbie though!

Where would I need to add the references to my sheets?

Does this get declared in the General or as a Private Sub?

Sorry.
 

jkpieterse

Well-known Member
Joined
Dec 3, 2007
Messages
1,155
The code refreshes all queries and pivot tables of the active workbook.
Alt+F11 to go to the VBA editor, Insert, Module to insert a module, paste in this code.
Alt+F8 from Excel gives you the list of macros, select thisone and click run. ALternatively, add a Forms button to a sheet and assign this macro to it.
 

dibblejon

New Member
Joined
Sep 23, 2010
Messages
37

ADVERTISEMENT

Thank you very much for your help.

I have done as you advised but when I run the macro nothing happens.

My "Data" table uses parameters when refreshing - would this cause the macro to not run?
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
You can step through the code by putting your cursor inside the code block and hitting F8 - then keep hitting F8 to execute one line at a time. This should show you what the code is doing (as described here) and whether the refresh commands are being executed.

Not sure but for Excel 2007 or 2010 you might need different syntax for query tables (as members of the list object collection).

Untested excel 2007/2010 code below. I suppose it could error if there are listobjects without query tables ...
Code:
Public Sub foo()
Dim ws As Worksheet
Dim lo As ListObject
Dim qt As QueryTable
Dim pc As PivotCache

For Each ws In ThisWorkbook.Worksheets
    For Each lo In ws.ListObjects
         lo.QueryTable.Refresh False
    Next lo
Next ws

For Each pc In ActiveWorkbook.PivotCaches
    pc.Refresh
Next

End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,199
Messages
5,857,888
Members
431,905
Latest member
RW11700

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
Top