+ Reply to Thread
Results 1 to 10 of 10

Updating data source and refreshing all pivot tables through VBA.

Hybrid View

Folshot Updating data source and... 11-24-2013, 12:28 PM
AlphaFrog Re: Updating data source and... 11-24-2013, 12:59 PM
Folshot Re: Updating data source and... 11-24-2013, 01:27 PM
AlphaFrog Re: Updating data source and... 11-24-2013, 01:57 PM
Folshot Re: Updating data source and... 11-24-2013, 02:46 PM
AlphaFrog Re: Updating data source and... 11-24-2013, 03:11 PM
Folshot Re: Updating data source and... 11-24-2013, 06:22 PM
Folshot Re: Updating data source and... 11-25-2013, 07:09 PM
AlphaFrog Re: Updating data source and... 11-26-2013, 02:41 AM
Folshot Re: Updating data source and... 11-26-2013, 10:01 AM
  1. #1
    Registered User
    Join Date
    09-16-2010
    Location
    Glasgow
    MS-Off Ver
    2010
    Posts
    68

    Updating data source and refreshing all pivot tables through VBA.

    Hi all,

    I have a spreadsheet containing several pivot tables, all based on the same data source. From time to time, the data source is updated is additional rows (column A never contains any empty cells), therefore I created the following macro to update the data source of all pivot tables as well as refreshing those:

    Sub Refresh_All_Pivot_Tables()
    
    Application.ScreenUpdating = False
    
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim pt As PivotTable
    
    Set wb = ActiveWorkbook
    SourceName = Worksheets("MasterSheet").Range("A1:AX1").End(xlDown)
    
      For Each ws In wb.Worksheets
        For Each pt In ws.PivotTables
          pt.ChangePivotCache _
            wb.PivotCaches.Create(SourceType:=xlDatabase, _
                  SourceData:=SourceName)
        Next pt
      Next ws
    
    ActiveWorkbook.RefreshAll
    
    Application.ScreenUpdating = True
    
    End Sub
    However it seems to fail when applying the SourceName variable. I am not sure where the issue could lie, I used F8 and watched the variables but the macro seems to do something completely different than what at I am expecting (i.e. simply update the source of each pivot tables using range A1:AX1 to the bottom of the 'MasterSheet' worksheet).

    Any help would be appreciated. Thanks.

    PS: I also used

    PT.SourceData = SourceName
    instead of

    wb.PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:=SourceName)
    but this did not help.
    Last edited by Folshot; 11-24-2013 at 12:30 PM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Updating data source and refreshing all pivot tables through VBA.

    Try this...

    Sub Refresh_All_Pivot_Tables()
        
        Application.ScreenUpdating = False
        
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim pt As PivotTable
        Dim SourceName As Range
        
        Set wb = ActiveWorkbook
        Set SourceName = Worksheets("MasterSheet").Range("A1:AX1").End(xlDown)
        
          For Each ws In wb.Worksheets
            For Each pt In ws.PivotTables
              pt.ChangePivotCache _
                wb.PivotCaches.Create(SourceType:=xlDatabase, _
                      SourceData:=SourceName)
            Next pt
          Next ws
        
        ActiveWorkbook.RefreshAll
        
        Application.ScreenUpdating = True
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    09-16-2010
    Location
    Glasgow
    MS-Off Ver
    2010
    Posts
    68

    Re: Updating data source and refreshing all pivot tables through VBA.

    Thanks AlphaFrog,

    The changes make sense but unfortunately the same issue remains, i.e. the macro stops when processing:

    pt.ChangePivotCache _
    wb.PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:=SourceName)
    I have attached a cutdown version of the spreadsheet containing the macro, not sure if this helps.

    Test5.xls

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Updating data source and refreshing all pivot tables through VBA.

    I got this to work with your Test5 workbook.

    Sub Refresh_All_Pivot_Tables()
        
        Dim Source As String
        Dim pc As PivotCache
        
        Application.ScreenUpdating = False
        
        Source = Worksheets("MasterSheet").Range("A1").CurrentRegion.Address(True, True, xlR1C1, True)
        
        For Each pc In ActiveWorkbook.PivotCaches
            pc.SourceData = Source
        Next
        
        ActiveWorkbook.ShowPivotTableFieldList = False
        Application.CommandBars("PivotTable").Visible = False
        ActiveWorkbook.RefreshAll
        Application.ScreenUpdating = True
        
    End Sub

  5. #5
    Registered User
    Join Date
    09-16-2010
    Location
    Glasgow
    MS-Off Ver
    2010
    Posts
    68

    Re: Updating data source and refreshing all pivot tables through VBA.

    I got this to work with your Test5 workbook.
    Could you please upload the version of the spreadsheet you now have? I have cut & pasted the last set of code you provided but still encounter the same issue

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Updating data source and refreshing all pivot tables through VBA.

    Quote Originally Posted by Folshot View Post
    Could you please upload the version of the spreadsheet you now have? I have cut & pasted the last set of code you provided but still encounter the same issue
    Here it is.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    09-16-2010
    Location
    Glasgow
    MS-Off Ver
    2010
    Posts
    68

    Re: Updating data source and refreshing all pivot tables through VBA.

    That's really strange, the macro you have created in the latest Test5.xls file seems to work fine for me, but if I cut & paste the code into my file (which contains exactly the same source data and pivot tables) or any other file, I get again the run time error 1004... really weird... I am currently investigating.

  8. #8
    Registered User
    Join Date
    09-16-2010
    Location
    Glasgow
    MS-Off Ver
    2010
    Posts
    68

    Re: Updating data source and refreshing all pivot tables through VBA.

    I am now using this code, it never generates any errors and seem to work fine:

    Sub Refresh_All_Pivot_Tables()
        
        Application.ScreenUpdating = False
        
        Dim ws As Worksheet
        Dim PT As PivotTable
        Dim Source As String
        
        Source = Worksheets("MasterSheet").Range("A1:D1").CurrentRegion.Address(True, True, xlR1C1, True)
           
        For Each ws In ActiveWorkbook.Worksheets
        For Each PT In ws.PivotTables
            PT.SourceData = Source
        Next PT
        Next ws
    
        
        ActiveWorkbook.ShowPivotTableFieldList = False
        Application.CommandBars("PivotTable").Visible = False
        ActiveWorkbook.RefreshAll
        Application.ScreenUpdating = True
        
    End Sub
    I have one outstanding issue though, I want the new range to be from columns "A1:D1" until last row, but the above code select the whole table, i.e. from "A1:J1) until the last row.

    I used this code as an alternative but this did not help:

        Dim ws As Worksheet
        Dim PT As PivotTable
        Dim LastRow As Long
        Dim Source As String
        
        LastRow = Worksheets("MasterSheet").Range("A" & Rows.Count).End(xlUp).Row
        Source = Worksheets("MasterSheet").Range("A1:D1" & LastRow).CurrentRegion.Address(True, True, xlR1C1, True)
           
        For Each ws In ActiveWorkbook.Worksheets
        For Each PT In ws.PivotTables
            PT.SourceData = Source
        Next PT
        Next ws
    Any idea and making sure the range is only for specific columns rather than all of them?

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Updating data source and refreshing all pivot tables through VBA.

    Remove the .CurrentRegion property and the 1 from D1


        LastRow = Worksheets("MasterSheet").Range("A" & Rows.Count).End(xlUp).Row
        Source = Worksheets("MasterSheet").Range("A1:D1" & LastRow).CurrentRegion.Address(True, True, xlR1C1, True)
        LastRow = Worksheets("MasterSheet").Range("A" & Rows.Count).End(xlUp).Row
        Source = Worksheets("MasterSheet").Range("A1:D" & LastRow).Address(True, True, xlR1C1, True)

  10. #10
    Registered User
    Join Date
    09-16-2010
    Location
    Glasgow
    MS-Off Ver
    2010
    Posts
    68

    Re: Updating data source and refreshing all pivot tables through VBA.

    Thanks AlphaFrog, it works perfect!

+ 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. Change Pivot Source Data in multiple pivot tables
    By jacol in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 05-20-2014, 06:18 AM
  2. Replies: 3
    Last Post: 02-27-2012, 08:03 PM
  3. Pivot Tables and VBA: Changing Contents in Data Area and updating the Data Source
    By kyrgyzstanart in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-22-2011, 11:11 AM
  4. updating pivot tables using dynamic data source
    By dab4211 in forum Excel General
    Replies: 3
    Last Post: 07-24-2006, 04:45 PM
  5. [SOLVED] Updating source data while in Pivot Table
    By Carim in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-27-2005, 10:06 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