Consulting

Results 1 to 5 of 5

Thread: Updating Source Range of Pivot Tables

  1. #1
    VBAX Regular
    Joined
    May 2004
    Location
    Driffield, East Yorkshire, Egnland
    Posts
    69
    Location

    Updating Source Range of Pivot Tables

    Hi folks... hopefully this is a quick question which can give a quick answer.

    I have a spreadsheet that contains, among other things, a sheet that is updated via another process (gets a few hundred rows added). I have a pivot table that is based on this data, and I need the pivot table to detect the new records and refresh accordingly.

    The following code snippet is what I've got in place currently - problem is though it does NOT update the range.


    'Create a pivot table based on the data...
        Application.StatusBar = "Updating Pivot, please wait..."
        Sheets("Pivots").Select
        Sheets("Pivots").Activate
        With Sheets("Pivots")
        .PivotTableWizard SourceType:=xlDatabase, SourceData:="AllData!A1:E" & lngAllMaxRow + 2
        .PivotTables("CRTGDemand").Refresh
        End With
    Application.CommandBars("PivotTable").Visible = False
    The variable lngAllMaxRow contains the total number of data rows on the sheet AllData. The range A1:Exxx is the range I want the pivot table to reflect.

    Any ideas? This is going to bug me, I can see...

    Cheers,

    Ad

  2. #2
    BoardCoder
    Licensed Coder
    VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    This should do it:

    Dim ws As Worksheet
        Set ws = ActiveSheet
        Dim p As PivotTable
        Set p = ws.PivotTables("CRTGDemand")
        p.SourceData = "AllData!A1:E" & lngAllMaxRow + 2
        p.RefreshTable

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  3. #3
    VBAX Regular
    Joined
    May 2004
    Location
    Driffield, East Yorkshire, Egnland
    Posts
    69
    Location
    Simply stunning, thanks Mark.

    Ad

  4. #4
    BoardCoder VBAX Regular
    Joined
    May 2004
    Location
    Istanbul, Turkiye
    Posts
    73
    Quote Originally Posted by Adaytay
    Hi folks... hopefully this is a quick question which can give a quick answer.

    I have a spreadsheet that contains, among other things, a sheet that is updated via another process (gets a few hundred rows added). I have a pivot table that is based on this data, and I need the pivot table to detect the new records and refresh accordingly.

    The following code snippet is what I've got in place currently - problem is though it does NOT update the range.

     'Create a pivot table based on the data...
        Application.StatusBar = "Updating Pivot, please wait..."
        Sheets("Pivots").Select
        Sheets("Pivots").Activate
        With Sheets("Pivots")
            .PivotTableWizard SourceType:=xlDatabase, SourceData:="AllData!A1:E" & lngAllMaxRow + 2
            .PivotTables("CRTGDemand").Refresh
        End With
    Application.CommandBars("PivotTable").Visible = False
    The variable lngAllMaxRow contains the total number of data rows on the sheet AllData. The range A1:Exxx is the range I want the pivot table to reflect.

    Any ideas? This is going to bug me, I can see...

    Cheers,

    Ad
    Hi,

    Different approach: I would use a dynamic range as pivot table source so I wouldn't bother with changing its source but only refresh it either manually or via simple VBA code.

    Please see the attachment.

    Suat

  5. #5
    MS Excel MVP VBAX Regular Ken Wright's Avatar
    Joined
    Jun 2004
    Posts
    17
    Location
    Agree with Smozgur - Dynamic Source range has to be a better way than re-doing the source range by code:-

    http://www.contextures.com/xlPivot01.html#Dynamic

    Regards
    Ken..................
    It's easier to beg forgiveness than ask permission

Posting Permissions

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