Consulting

Results 1 to 8 of 8

Thread: Dynamic Table Array for Pivot Table in VBA

  1. #1

    Dynamic Table Array for Pivot Table in VBA

    Hello experts,

    a little new at this but i have done all the research i can and cannot pinpoint the solution.

    i would like to build a macro to create a pivot table on a data set that grows in length each week. The part i am having trouble with is having the pivot table include the entire data set each time. The SourceData section stays static at "Sheet1!R1C1:R26958" instead of including the new table size.

        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "Sheet1!R1C1:R26958", Version:=xlPivotTableVersion14).CreatePivotTable _
            TableDestination:="Sheet2!R3C1", TableName:="PivotTable1", DefaultVersion _
            :=xlPivotTableVersion14
    thoughts? Thank you in advance!

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    you want to create new pivot table every week?
    or only updating?

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    As mana says, perhaps you don't need to re-create the pivot table from scratch - just change the data it's looking at.
    You might get away with just:
    Sub blah()
    With Sheets("Sheet2").Range("A3").PivotTable
      .ChangePivotCache ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("Sheet1").Range("A1").CurrentRegion, Version:=xlPivotTableVersion14)
      .PivotCache.Refresh
    End With
    End Sub
    However, is it actually an Excel Table that gets updated every time - does it keep its name? If so then you need just make sure that the datasource for the pivot is the name of the table, something like Table1 (not in code, just through the dialogue boxes). Then after updating the table you'll only have to refresh the pivot.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    its a brand new set of data every week so I'd need to create from scratch each time.

    i tried creating a data table each time as well, but i run into the same issue where it hard codes the table range in the macro.

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by QnA View Post
    its a brand new set of data every week so I'd need to create from scratch each time.
    Not necessarily; does the new set of data have the same headings?
    If so running the blah macro in my last message should work (I derived where the pivot table is and where the source data is from your quoted code).
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    the issue is that there is no existing pivot table. i would run a report on a data set, then the following week, there is an entirely new file that i would need to run the report on again. there is no existing Pivot table.

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Change that line in your first post here to:
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    Sheets("Sheet1").Range("A1").CurrentRegion, Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="Sheet2!R3C1", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion14
    but make sure you've a blank sheet2 before you start (and no other pivot table called PivotTable1 in the workbook)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    You are some kind of wonderful P45Cal! That worked perfectly! Thank you!

Posting Permissions

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