+ Reply to Thread
Results 1 to 11 of 11

VBA code to auto refresh Pivot table

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    112

    VBA code to auto refresh Pivot table

    Hello Excel Gurus,

    I need help to come up with a VBA code to automatically refresh the Pivot table every time there is a change in the source sheet. Also, if a new item is added, it must be included in the Pivot table. Refreshes maybe by row or block whichever is efficient. Code maybe triggered by selecting any sheet. I have 10,000 rows and it will grow in time.

    Sheet 1 (Trans sheet) locked sheet
    This is the source sheet that all transaction is being keyed.
    Column P= Item No.
    Q = REC
    R = ISS
    S = ADJ

    Sheet 2 (Masterfile sheet) Pivot table, locked sheet
    Column A= Item No.
    B = sum of REC
    C = sum of ISS
    D = sum of ADJ

    Thank you in advance.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VBA code to auto refresh Pivot table

    You need to record a macro refreshing the pivot then call it from the Worksheet_Change event of the sheet the data is in.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Contributor
    Join Date
    12-01-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: VBA code to auto refresh Pivot table

    Thanks for your prompt reply.

    I am a newbie and a very little knowedge on VBA and i would appreciate if you can write a code for me.

    Thank you.

  4. #4
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: VBA code to auto refresh Pivot table

    Hi try ..

    Sub MyMacro()
    'My code....
    ActiveWorkbook.RefreshAll
    'Or
    'ThisWorkbook.RefreshAll
    End Sub
    Read...
    http://www.ozgrid.com/VBA/pivot-table-refresh.htm
    "No xadrez nem sempre a menor dist?ncia entre dois pontos ? uma linha reta" G. Kasparov.

    If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select b from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VBA code to auto refresh Pivot table

    Not really, as i dont know the names of the pivot,.

    in Trans sheet

    you will trigger the code from the Worksheet_Change() event, use the drop downs at the top of the code window.

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

    Re: VBA code to auto refresh Pivot table

    Do you mean you need to expand the no of rows the pivot table is using as it's source?

    If you do i would suggest you convert the data on 'Trans sheet' to table format and then redoing the pivot table.

    Then you should be able to use something like this.
    Sheets("Masterfile").PivotTables(1).PivotCache.Refresh ' change Masterfile if that's not the actual name with the pivto table on it.
    If posting code please use code tags, see here.

  7. #7
    Forum Contributor
    Join Date
    12-01-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: VBA code to auto refresh Pivot table

    I want that macro be triggered (Refresh pivot) when Masterfile sheet is selected.

  8. #8
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: VBA code to auto refresh Pivot table

    Inside sheet module
    Private Sub Worksheet_Activate()
    'YourCode here
    End Sub

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA code to auto refresh Pivot table

    Hi,

    The first thing to do is apply a dynamic range name to the source sheet data. Typically this name would be defined something like

    =Offset(SourceSheet!$A$1,0,0,COUNTA(SourceSheet!$A:$A),COUNTA(SourceSheet!$1:$1)

    Then just use the range name in the PT Source Source Data.

    I tend to use the Activate event on the sheet that contains the PT to refresh the PT, i.e.

    ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

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

    Re: VBA code to auto refresh Pivot table

    Richard

    Wouldn't converting the data in to a table/listobject do much the same thing as setting up a dynamic named range?

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA code to auto refresh Pivot table

    @Norie,

    Yes indeed. Thanks for picking that up.

    Although I must admit I tend to avoid the 'new' Table functionality, I just don't like the way it complicates formulae when you need to refer to columns in the Table. It just doesn't seem as user friendly somehow which is why I tend to forget about it when I would agree that it does offer another way forward.

+ 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. import data from access table to excel pivot table - Enable Auto Refresh
    By okl in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 02-01-2010, 09:38 AM
  2. [SOLVED] Auto-refresh pivot table?
    By kk_oop@yahoo.com in forum Excel General
    Replies: 1
    Last Post: 04-18-2006, 05:35 PM
  3. Replies: 6
    Last Post: 12-21-2005, 07:20 PM
  4. [SOLVED] auto refresh of pivot table
    By Luc Poppe in forum Excel General
    Replies: 9
    Last Post: 09-10-2005, 01:05 PM
  5. Pivot Table - Auto Refresh
    By mdalby in forum Excel General
    Replies: 1
    Last Post: 04-11-2005, 08:14 PM

Tags for this Thread

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