+ Reply to Thread
Results 1 to 2 of 2

Change Data Source of Pivot Tables, Omit Blank Rows/Columns

  1. #1
    Forum Contributor
    Join Date
    10-24-2012
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    408

    Change Data Source of Pivot Tables, Omit Blank Rows/Columns

    Hello,
    I have a code shown below that allows me to change the source data of all pivot tables in a given workbook. It works fine, but I would like it to select all the data to the last row and last column in that targeted sheet. In other words, I don't want it to select blank rows and columns.

    Converting the source data into a table is not an option in this instance.

    Sub ChangeDataSourceForAllPivotTables()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim sSourceData As String

    On Error GoTo ErrHandler

    sSourceData = "'C:\Blah\Blah\Blah\Blah\Blah\[Blah_Blah.xlsm]BLAH'!A1:v100000"

    Set wb = ActiveWorkbook

    For Each ws In wb.Worksheets
    For Each pt In ws.PivotTables
    pt.ChangePivotCache wb.PivotCaches.Create(xlDatabase, sSourceData)
    pt.RefreshTable
    Next pt
    Next ws

    ExitTheSub:

    Set wb = Nothing
    Set ws = Nothing
    Set pt = Nothing

    Exit Sub

    ErrHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Error"
    Resume ExitTheSub

    End Sub

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Change Data Source of Pivot Tables, Omit Blank Rows/Columns

    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.

    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



    This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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 data source of pivot tables and refresh them
    By yhyhyhyh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-31-2014, 04:12 AM
  2. 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
  3. Re: Pivot Tables,Sliders, blank rows and expanding source data help
    By Jaime1234 in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 09-09-2013, 12:06 PM
  4. Change Data Source for Multiple Pivot Tables Simultaneously
    By mshirschy in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 02-11-2013, 02:31 PM
  5. Replies: 3
    Last Post: 02-27-2012, 08:03 PM
  6. Excel 2007 : Change data source in 2007 pivot tables
    By Portuga in forum Excel General
    Replies: 4
    Last Post: 11-24-2011, 02:50 AM
  7. Change multiple pivot tables data source..
    By benclayes in forum Excel General
    Replies: 3
    Last Post: 11-06-2010, 12:46 PM

Tags for this Thread

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