+ Reply to Thread
Results 1 to 17 of 17

PivotTable auto refresh

  1. #1
    Registered User
    Join Date
    12-05-2014
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    18

    PivotTable auto refresh

    I've found this code on the internet and can't get it to work properly.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim pt As PivotTable
    Set pt = Worksheets("Pivot table").PivotTables("PivotTable1")
    If Intersect(Target, pt.TableRange1) Is Nothing Then
    Worksheets("Pivot table").PivotTables("PivotTable1").PivotCache.Refresh
    End If
    End Sub

    My PivotTable & Chart are on the same sheet ('data sheet') as my reference table. The reference table takes its info from 14 other sheets. When I update one of the other sheets, the reference table on the 'data sheet' updates without problem. But the PivotTable & Chart will only update if a change is manually made to the 'data sheet'.

    Can someone recommend any tweaks to this code to get the PivotTable & Chart to update when changes are made on the other sheets please?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: PivotTable auto refresh

    You could use the change event of the other sheets, or the workbook SheetChange event which is triggered when a change occurs on any sheet in the workbook.

    For example.
    Please Login or Register  to view this content.
    If you only want the pivot table to be refreshed when specific sheets are changed you could add conditons to the above code.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-05-2014
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    18

    Re: PivotTable auto refresh

    Thanks for the reply Norie, however this doesn't work

    With the code I showed, when a change is made on the data sheet, the PivotTable would update. Unfortunately with your code, the PivotTable won't update after any manual changes are made.

    Do you have any other suggestions please?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: PivotTable auto refresh

    Where did you put the code I posted?

    Are you making manual changes in the sheets the pivot/reference table is using for data?

  5. #5
    Registered User
    Join Date
    12-05-2014
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    18

    Re: PivotTable auto refresh

    I replaced my previous code with it, in the data sheet tab.

    At the moment the reference table for the pivottable is auto generated when data on the other sheets is manually changed. I'd like the pivottable to auto update once the reference table has been updated. The code I posted lets the pivottable update, when any manual change is done on that data sheet.

    This is my first go at VBA/ Macro etc, so apologies if I'm mixing anything up

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: PivotTable auto refresh

    The code goes in the ThisWorkbook module.

  7. #7
    Registered User
    Join Date
    12-05-2014
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    18

    Re: PivotTable auto refresh

    Thanks. I've put it in there now and I get a run-time error on the second last line - Worksheets("November Data").PivotTables("PivotTable8").PivotCache.Refresh

    run time error.jpg

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: PivotTable auto refresh

    Why not use the Activate event of the sheet with the pivot table/chart on to update them?
    Remember what the dormouse said
    Feed your head

  9. #9
    Registered User
    Join Date
    12-05-2014
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    18

    Re: PivotTable auto refresh

    Thanks for the reply romperstomper. Could you let me know what/ where I'd need to input for that to work?

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: PivotTable auto refresh

    Right-click the tab of the sheet containing the pivot table, choose View Code, then paste this in:
    Please Login or Register  to view this content.
    Now the pivot will be updated every time you switch back to this sheet after altering data on the other sheet(s).

  11. #11
    Registered User
    Join Date
    12-05-2014
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    18

    Re: PivotTable auto refresh

    Thats worked perfectly!!!

    Many thanks for your help (and for your help also Norie)

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: PivotTable auto refresh

    Glad to assist.

  13. #13
    Registered User
    Join Date
    12-05-2014
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    18

    Re: PivotTable auto refresh

    Could I bend your ear one more time please, on a slightly different problem

    I have another spreadsheet with each sheet having its on PivotTable. Can you recommend how I can get the PivotTable to auto update, when the data that has been changed is on the same sheet as the PivotTable?

  14. #14
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: PivotTable auto refresh

    In the ThisWorkbook module of the workbook, add this:
    Please Login or Register  to view this content.
    This assumes only one pivot table per sheet.

  15. #15
    Registered User
    Join Date
    12-05-2014
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    18

    Re: PivotTable auto refresh

    Many thanks again. This works great when the data is manually entered. However...

    I'm using 2 tables on one sheet. The first table contains a lot of data pulled from sharepoint, which has filters. I've set up another table to pull info from this, which the PivotTable pulls its data from.

    When I change the filter on my first table, the 2nd table updates as it should, but the PivotTable isn't reading it. Interestingly though, the PivotChart keeps the old quantities but the bars don't show any info...

    Bef Aft.jpg

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: PivotTable auto refresh

    The change event isn't triggered by formula calculations. If the source data is formula based, use:
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    12-05-2014
    Location
    UK
    MS-Off Ver
    Office 2010
    Posts
    18

    Re: PivotTable auto refresh

    Thanks again

    That's brilliant and works like a charm. Really appreciate your help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 01-07-2013, 08:39 PM
  2. PivotTable/auto refresh
    By Trainee in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-16-2011, 05:19 PM
  3. Enable Auto Refresh - Wait for refresh to sinish before running a macro
    By tomlancaster in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-21-2011, 11:52 AM
  4. PivotTable.Refresh woes
    By JeremyR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-31-2007, 09:04 PM
  5. PivotTable Does not refresh individual items
    By Nick O in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-23-2005, 06:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1