+ Reply to Thread
Results 1 to 4 of 4

vba - best practice for re-setting the data range for pivots

Hybrid View

  1. #1
    Registered User
    Join Date
    05-05-2014
    Location
    Ventura
    MS-Off Ver
    Excel 2010
    Posts
    24

    vba - best practice for re-setting the data range for pivots

    Hello: I have a workbook that has many worksheet. Each worksheet has one pivot.
    The datasource for the pivots is a range - pulled from another worksheet in the same workbook.
    I have a macro that gets new records from an sql server and appends them to a queryTable for the datasource,
    and so I need to re-set the data range for my pivots when this is run.

    My code is working; however, it takes 3 min to run. I have 7 worksheets so far. I was wondering if
    my code looked most efficient....maybe there's a way to help it to run a bit faster?

    thank you for your help,
    Proctor

    Sub RefreshALLPivotTables()
     Dim WS As Worksheet
     For Each WS In ThisWorkbook.Worksheets
            For Each pt In WS.PivotTables
            'MsgBox ("pt.name " & pt.Name)
                pt.SourceData = Sheets("ShipData").[ShipData!$A$1:$W$1].CurrentRegion.Address(True, True, xlR1C1, True)
                'pt.RefreshTable
            Next pt
            Set pt = Nothing
     Next WS
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: vba - best practice for re-setting the data range for pivots

    So I started using Dynamic Named Ranged instead of VBA to reset the names.

    Named Range
    PivotDATA = offset(A1,0,0,counta(A:A),counta(1:1))

    Then I just link the pivot to the PivotDATA named range and I never think about it again.
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Registered User
    Join Date
    05-05-2014
    Location
    Ventura
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: vba - best practice for re-setting the data range for pivots

    NameManager.JPG

    mikeTRON: Thank you so much for your post!!! I gave it a try and it works great!

    Just to verify, after I run my vba code that appends data to my Dynamic named range, I need to click the refresh button to
    have that data show up in my pivots?

    I wanted to verify, because when I don't hit refresh, it doesn't show up....believe this is how it's suppose to work.

    There's one concern, after I run my vba code that appends data, if I look under Formulas-> named manager (see pic)
    I noticed that along with my newly created Dynamic named range "ShipData", it was creating another Dynamic named range. When I clicked in the "Refers to:"
    textbox, I noticed it was referring to the newly appended data; however, I don't believe I need this named range, because my "ShipData" gets everything.

    I stepped through my code and noticed it was adding this named range when it hits the line "Refresh BackGroundQuery: False"
    I need this line in order for the new data to appear in the datasheet.

    I wanted to know what your thoughts are on it.

    Thank you again for providing me with this solution.
    Proctor

  4. #4
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: vba - best practice for re-setting the data range for pivots

    So I cant really help diagnose your problem, but I do know once you setup the dynamic named range, all you have to do is update the data and
    thisworkbook.refreshall
    to have the pivot table update.


    I would remove this from the code after you initially setup the datasource:
    pt.SourceData = Sheets("ShipData").[ShipData!$A$1:$W$1].CurrentRegion.Address(True, True, xlR1C1, True)

+ 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. [SOLVED] Setting Variables - best practice
    By BremhillBob in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-15-2014, 03:15 PM
  2. Replies: 0
    Last Post: 10-18-2013, 11:35 AM
  3. Replies: 0
    Last Post: 02-13-2012, 12:58 PM
  4. Replies: 2
    Last Post: 02-10-2012, 04:12 PM
  5. General Navigation Best Practice Question: Application.GoTo vs. Range.Select
    By AlvaroSiza in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-09-2011, 09:31 PM

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