Changing data source of pivot tables with VBA The Next CEO of Stack OverflowExcel 2007: Move...

Towers in the ocean; How deep can they be built?

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Strange use of "whether ... than ..." in official text

Cannot shrink btrfs filesystem although there is still data and metadata space left : ERROR: unable to resize '/home': No space left on device

Is it okay to majorly distort historical facts while writing a fiction story?

How do I fit a non linear curve?

Is it convenient to ask the journal's editor for two additional days to complete a review?

The Ultimate Number Sequence Puzzle

What are the unusually-enlarged wing sections on this P-38 Lightning?

Is there a way to save my career from absolute disaster?

Raspberry pi 3 B with Ubuntu 18.04 server arm64: what chip

In the "Harry Potter and the Order of the Phoenix" video game, what potion is used to sabotage Umbridge's speakers?

What's the commands of Cisco query bgp neighbor table, bgp table and router table?

Is it professional to write unrelated content in an almost-empty email?

Computationally populating tables with probability data

Is it ok to trim down a tube patch?

Aggressive Under-Indexing and no data for missing index

What happened in Rome, when the western empire "fell"?

Do scriptures give a method to recognize a truly self-realized person/jivanmukta?

Can this note be analyzed as a non-chord tone?

Won the lottery - how do I keep the money?

Why do we say 'Un seul M' and not 'Une seule M' even though M is a "consonne"

My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?

Is it ever safe to open a suspicious HTML file (e.g. email attachment)?



Changing data source of pivot tables with VBA



The Next CEO of Stack OverflowExcel 2007: Move the connection of an existing pivot table from a data connection to an Excel tableHow can I use indirect references for a pivot table's data source?Auto formatting new pivot rows to data barsSheet name dynamically assigned from a cell valueTwo-way data update with Excel PowerPivot tables (Office 2013)Excel VBA: Creating Hyperlinks Type MismatchDelete multiple Columns with simple macroExcel VBA - Copy cell values *without* data validationcombine data with different headerVBA code that skips a row instead of “For Each row In rng.Rows”












0















I'm looking for a code that can change the data source of my pivot tables from one sheet with a different data source.
The data source sheet is identical just with different data.



I have a code that i've taken from the internet, but instead of changing the data source of my active sheet, it changes the data source on all my sheets.



Sadly i don't know much about VBA coding to know how to change the code to affect only my active sheet and i was hoping someone could help me.



Option Explicit

Sub ChangeDataSourceForAllPivotTables()

Dim wb As Workbook
Dim ws As Worksheet
Dim pt As PivotTable
Dim rSourceData As Range

If ActiveWorkbook Is Nothing Then Exit Sub

On Error GoTo ErrHandler

Set wb = ActiveWorkbook

Set rSourceData = wb.Worksheets("Sheet1").Range("A1").CurrentRegion 'change the name of the worksheet accordingly

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

ExitTheSub:
Set wb = Nothing
Set ws = Nothing
Set pt = Nothing
Set rSourceData = Nothing

Exit Sub

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

End Sub


Thank you in advance for the help!










share|improve this question



























    0















    I'm looking for a code that can change the data source of my pivot tables from one sheet with a different data source.
    The data source sheet is identical just with different data.



    I have a code that i've taken from the internet, but instead of changing the data source of my active sheet, it changes the data source on all my sheets.



    Sadly i don't know much about VBA coding to know how to change the code to affect only my active sheet and i was hoping someone could help me.



    Option Explicit

    Sub ChangeDataSourceForAllPivotTables()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim rSourceData As Range

    If ActiveWorkbook Is Nothing Then Exit Sub

    On Error GoTo ErrHandler

    Set wb = ActiveWorkbook

    Set rSourceData = wb.Worksheets("Sheet1").Range("A1").CurrentRegion 'change the name of the worksheet accordingly

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

    ExitTheSub:
    Set wb = Nothing
    Set ws = Nothing
    Set pt = Nothing
    Set rSourceData = Nothing

    Exit Sub

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

    End Sub


    Thank you in advance for the help!










    share|improve this question

























      0












      0








      0








      I'm looking for a code that can change the data source of my pivot tables from one sheet with a different data source.
      The data source sheet is identical just with different data.



      I have a code that i've taken from the internet, but instead of changing the data source of my active sheet, it changes the data source on all my sheets.



      Sadly i don't know much about VBA coding to know how to change the code to affect only my active sheet and i was hoping someone could help me.



      Option Explicit

      Sub ChangeDataSourceForAllPivotTables()

      Dim wb As Workbook
      Dim ws As Worksheet
      Dim pt As PivotTable
      Dim rSourceData As Range

      If ActiveWorkbook Is Nothing Then Exit Sub

      On Error GoTo ErrHandler

      Set wb = ActiveWorkbook

      Set rSourceData = wb.Worksheets("Sheet1").Range("A1").CurrentRegion 'change the name of the worksheet accordingly

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

      ExitTheSub:
      Set wb = Nothing
      Set ws = Nothing
      Set pt = Nothing
      Set rSourceData = Nothing

      Exit Sub

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

      End Sub


      Thank you in advance for the help!










      share|improve this question














      I'm looking for a code that can change the data source of my pivot tables from one sheet with a different data source.
      The data source sheet is identical just with different data.



      I have a code that i've taken from the internet, but instead of changing the data source of my active sheet, it changes the data source on all my sheets.



      Sadly i don't know much about VBA coding to know how to change the code to affect only my active sheet and i was hoping someone could help me.



      Option Explicit

      Sub ChangeDataSourceForAllPivotTables()

      Dim wb As Workbook
      Dim ws As Worksheet
      Dim pt As PivotTable
      Dim rSourceData As Range

      If ActiveWorkbook Is Nothing Then Exit Sub

      On Error GoTo ErrHandler

      Set wb = ActiveWorkbook

      Set rSourceData = wb.Worksheets("Sheet1").Range("A1").CurrentRegion 'change the name of the worksheet accordingly

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

      ExitTheSub:
      Set wb = Nothing
      Set ws = Nothing
      Set pt = Nothing
      Set rSourceData = Nothing

      Exit Sub

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

      End Sub


      Thank you in advance for the help!







      microsoft-excel pivot-table






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 13 mins ago









      Pop ClaudiuPop Claudiu

      83




      83






















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1419825%2fchanging-data-source-of-pivot-tables-with-vba%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1419825%2fchanging-data-source-of-pivot-tables-with-vba%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

          VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...

          Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...