Solved

Pivot table Refresh on File Open

Posted on 2010-08-27
13
681 Views
Last Modified: 2012-05-10
I am using Excel 2007.  I have a Pivot Table that is getting External data from SQL Server via Microsoft Query.  This uses a named connection and that connection has properties, including a Command Text Property. This contains the query string that pulling data in In my workbook on sheet1 which  I named  All_Data.
I have my pivot table on Sheet2 which I named Pivot. Source for my pivot table is Sheet1 – All_data and used  dynamic range which I named Pivot_Range.
 Pivot_Rage=All_Data!$A$1:INDEX(All_Data!$1:$1048576,MATCH("ZZZZZZ",All_Data!$A:$A), MATCH("ZZZZZZ",All_Data!$1:$1))
 
What I need is how to refresh my workbook on file open. I assume that when Sheet1- All_Data is refreshed that my pivot table on Sheet2- Pivot will be refreshed automatically.
I got some suggestion from
Calacuccia:
Private Sub Workbook_Open()
Me.Worksheets("Sheet1").PivotTables(1).PivotCache.Refresh
End Sub

 and MelindaJonson
Right click on pivot table
click PivotTable options
click on the Data tab
check the box "Refresh data when opening file"

However they not working.
I tried this too:
Me.Worksheets("All_Data").QueryTables(1).Refresh
Me.Worksheets("Pivot").PivotTables(1).RefreshTable
And on first line I got Run time error ‘9’ Subscript Out of range.

How to got this working in this scenario.
And how to get this working in scenario when I  have just one spreadsheet, that have just pivot table on  in my case worksheet “Pivot”  with out having worksheet All_Data which I use as source for my pivot table.

0
Comment
Question by:Taras
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
13 Comments
 
LVL 17

Expert Comment

by:calacuccia
ID: 33542596
Me.Worksheets("Sheet1").PivotTables(1).PivotCache.Refresh

Have you tried this exact command with "Sheet2"? (of course you have to give the exact name which you used for sheet2 (as your pivottable is in Sheet2)..
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33542623
Subscript out of range is often because your Worksheet name is not correct.
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 500 total points
ID: 33542738
Taras,If your linked data from SQL Server is being dumped into an Excel Table, then a extra layer is required.  The following code seems to be working for me.You may want to check out my article on enabling PivotTable updates, http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/A_3172-How-to-Automatically-Update-Your-PivotTables-and-PivotCharts.htmlJudging by the named range you implemented, though, it's possible you already have read it :)Patrick
Private Sub Workbook_Open()
    
    ' Note that while they are called "Tables" in the Excel UI and documentation,
    ' in VBA they are referred to as ListObjects...
    
    With Me
        .Worksheets("All_Data").ListObjects(1).QueryTable.Refresh
        .Worksheets("Pivot").PivotTables(1).RefreshTable
    End With
    
End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33542754
>>Subscript out of range is often because your Worksheet name is not correct.That was my first thought as well, but in this case it looks like a quirk of how ListObjects that are linked to external data sources behave :)
0
 

Author Comment

by:Taras
ID: 33542866
Calacuccia you or me missing something here, probably me.

Yes I gave exact name:
Me.Worksheets("Pivot").PivotTables(1).PivotCache.Refresh
It runs without error and it did what is saying to do. It refreshed  pivot table with data that are not refreshed means old data (from source that is not refreshed).
Source of pivot table is All_data Sheet1 and that Sheet1 is getting data from SQL Server query as I explained. Source of pivot table is not refreshed. Pivot table is just refreshed with old data from Sheet1-All_Data.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33542900
Ok, I am missing something, I believe Patrick is on the right track.
0
 

Author Comment

by:Taras
ID: 33543482
Patrick example is very good, however I can use It when I got data from SQL Server in my Workbook.
 To adjust my question and address problem properly, I need to ask question like this.
How I can refresh my data in Sheet1 – All_Data on open file with upper mentioned scenario. Forget pivot table.
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 33543851
By taking out the reference to the PivotTable:
Private Sub Workbook_Open()
    
    ' Note that while they are called "Tables" in the Excel UI and documentation,
    ' in VBA they are referred to as ListObjects...
    
    Me.Worksheets("All_Data").ListObjects(1).QueryTable.Refresh
    
End Sub

Open in new window

0
 

Author Comment

by:Taras
ID: 33544264
Patrick it is not working.
Simply , I need something that will do the same thing like :Go  to data tab in excel  menu and press Refress All .
After I press Refress All (ctr+Alt+F5)  it refreshed properly.
0
 

Author Comment

by:Taras
ID: 33544331
I found it and it is working.
Private Sub Workbook_Open()

ActiveWorkbook.RefreshAll
End Sub.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33544651
Nice & simple
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33544836
>>Patrick it is not working.Please explain what you mean by that, because it worked perfectly for me.
0
 

Author Closing Comment

by:Taras
ID: 33569267
Thanks a lot
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question