+ Reply to Thread
Results 1 to 2 of 2

Dynamically Change Every Pivot Table Data Source Range Inside A Workbook With This VBA Mac

  1. #1
    Registered User
    Join Date
    05-10-2017
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    3

    Dynamically Change Every Pivot Table Data Source Range Inside A Workbook With This VBA Mac

    Help!

    I am working with the following code that I found in my online search to help automatically refresh pivots linked to data that may expand, contract, etc. It isn't working and I am obviously missing something. I am using the following code, but honestly have no idea which fields I need to update with specific references to my personal workbook.

    ________________________________________________________________

    Sub AdjustAllPivotDataRanges()
    'PURPOSE: Dynamically change every pivot table's data source range in the workbook
    'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

    Dim sht As Worksheet
    Dim pvt As PivotTable
    Dim StartPoint As Range
    Dim rng As Range
    Dim SourceAddress As String

    'Enter Worksheet Name that holds your Pivot data source
    Set sht = ActiveWorkbook.Worksheets("Data")

    'Enter first cell in your Pivot data source
    Set StartPoint = sht.Range("C5")

    'Create SourceData address
    Set rng = sht.Range(StartPoint, StartPoint.SpecialCells(xlLastCell))
    SourceAddress = sht.Name & "Data" & rng.Address(ReferenceStyle:=xlR1C1)

    'Loop through and update pivot tables with new data source range
    For Each sht In ThisWorkbook.Worksheets
    For Each pvt In sht.PivotTables

    'Change Pivot Table's data source range address
    pvt.ChangePivotCache _
    ThisWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=SourceAddress)

    'Ensure Pivot Table is refreshed
    pvt.RefreshTable

    Next pvt
    Next sht

    'Completion Message
    MsgBox "All Pivot Table Data Source Ranges have been updated in this workbook!", vbInformation

    End Sub

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Dynamically Change Every Pivot Table Data Source Range Inside A Workbook With This VBA

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    obviously to make it relevant to your specific example we would need your specific example

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

    this aside
    why not just use a table as data source?
    it will automatically update your data source without the need for vba?
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

+ 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. Disconnect slicers, dynamically update pivot table data source range et reconnect
    By Vincent121 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-10-2016, 05:30 AM
  2. Change source of pivot table dynamically based on sheet name
    By jeroenft in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-03-2016, 11:23 AM
  3. Change Pivot Table Data Source based on Dynamic Range
    By hamidxa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2014, 04:46 PM
  4. [SOLVED] Change Pivot Table Data Source to range A7: last record in F column
    By johnstevens in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-09-2014, 08:35 AM
  5. Macro to change pivot table range after importing new source data
    By kevinm3u in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-07-2014, 12:27 PM
  6. DYNAMICALLY data source for pivot table
    By Kalyan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-12-2006, 11:10 PM
  7. [SOLVED] Change the range of a pivot table data source
    By Tony White in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-11-2005, 03:05 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