+ Reply to Thread
Results 1 to 5 of 5

VBA Pivot Table - Change Data Source

  1. #1
    Registered User
    Join Date
    09-06-2023
    Location
    Montreal, Quebec, Canada
    MS-Off Ver
    Microsoft 365 VBA
    Posts
    4

    Question VBA Pivot Table - Change Data Source

    Hi,

    This code has been produced by the Excel Macro Recorder. I'd like to change the R1C4:R6C5 (which is fixed) by some variable expression as the data to be shown in the Pivot Table varies depending on some logic (counters) in my macro. How can I make this field "variable"?


    Sheets("Résultats").Select
    ActiveSheet.PivotTables("Résultats").ChangePivotCache ActiveWorkbook. _
    PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "[TEST 2022-09 Outil de vote.xlsm]Votes!R1C4:R6C5" _
    , Version:=8)

    Regards.

    Icare.

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,984

    Re: VBA Pivot Table - Change Data Source

    One way you can do it is to use a named range for the pivot table source data. Then you only need to redefine the named range.

  3. #3
    Registered User
    Join Date
    09-06-2023
    Location
    Montreal, Quebec, Canada
    MS-Off Ver
    Microsoft 365 VBA
    Posts
    4

    Re: VBA Pivot Table - Change Data Source

    Hi ByteMarks,

    Thank you very much for your prompt answer. I will certainly try and do that.
    Regards

    Yves Carrier

  4. #4
    Registered User
    Join Date
    09-06-2023
    Location
    Montreal, Quebec, Canada
    MS-Off Ver
    Microsoft 365 VBA
    Posts
    4

    Re: VBA Pivot Table - Change Data Source

    Hi ByteMarks,

    I'm sorry to bother you again. Following your advice, I have been trying unsuccessfully to the make the pivot table data source variable. The first example below is working fine but it has a fixed number of rows and colums as R1C4:R76C5. In this case, the first 3 lines are useless; I kept them there just to keep my program identical in both cases.

    TotalDeVotes = 76
    Set RangeForPivot = Workbooks("TEST 2022-09 Outil de vote.xlsm").Sheets("Votes").Range("D1")
    Set RangeForPivot = RangeForPivot.Resize(TotalDeVotes, 2)
    Workbooks("TEST 2022-09 Outil de vote.xlsm").Worksheets("Résultats").Activate
    ActiveSheet.PivotTables("Résultats").ChangePivotCache ActiveWorkbook.PivotCaches _
    .Create(SourceType:=xlDatabase, SourceData:="[TEST 2022-09 Outil de vote.xlsm]Votes!R1C4:R76C5", Version:=8)

    The second example stops on a run time error "5".

    TotalDeVotes = 76
    Set RangeForPivot = Workbooks("TEST 2022-09 Outil de vote.xlsm").Sheets("Votes").Range("D1")
    Set RangeForPivot = RangeForPivot.Resize(TotalDeVotes, 2)
    Workbooks("TEST 2022-09 Outil de vote.xlsm").Worksheets("Résultats").Activate
    ActiveSheet.PivotTables("Résultats").ChangePivotCache ActiveWorkbook.PivotCaches _
    .Create(SourceType:=xlDatabase, SourceData:="[TEST 2022-09 Outil de vote.xlsm]Votes!RangeForPivot", Version:=8)

    The only difference is that I have changed the R1C4:R76C5 by a Named Range that I resized based on TotalDeVotes. The syntax or the logic of it does not work.

    If you have any other advice, I'll be glad to try it.

    Regards.

    Yves.

  5. #5
    Registered User
    Join Date
    09-06-2023
    Location
    Montreal, Quebec, Canada
    MS-Off Ver
    Microsoft 365 VBA
    Posts
    4

    Re: VBA Pivot Table - Change Data Source

    Hi ByteMarks,

    No need to worry anymore. I have found the solution. I just had to replace:

    SourceData:="[TEST 2022-09 Outil de vote.xlsm]Votes!RangeForPivot"

    by

    SourceData:=RangeForPivot

    Thanks for your support.

    Yves.

+ 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. Pivot Table: Can't change data source
    By CDaveBrown in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-08-2012, 01:12 PM
  2. Replies: 2
    Last Post: 04-16-2012, 05:06 PM
  3. Replies: 3
    Last Post: 02-27-2012, 08:03 PM
  4. Pivot table - change data source to another pivot table in 2010
    By thesecretsanta in forum Excel General
    Replies: 4
    Last Post: 04-13-2011, 12:54 PM
  5. VBA change pivot table source data?
    By skysurfer in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-14-2010, 09:14 PM
  6. Replies: 1
    Last Post: 06-22-2010, 09:10 AM
  7. Replies: 1
    Last Post: 06-20-2010, 04:00 AM
  8. change data source of pivot table
    By JohnH in forum Excel General
    Replies: 7
    Last Post: 08-17-2006, 12: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