Results 1 to 2 of 2
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Las Vegas, Nevada, USA
    Posts
    334
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Macro to create a pivot table

    I have recorded a macro to create a pivot table from data exported from one of our applications. The macro would select the Report Filter, Row Labels and Values. I carefully stepped through my notes while recording and now, in looking at the code I realize I will be stuck in only the second line of code.

    I preselected the range of data and fields title row but Excel has converted the range to SourceData:= "~PD1BE9!R7C1:R93236C22"

    My second problem will be name of the pivot table. In recording the macro Excel has assigned the code: TableName:="PivotTable3"

    How can I handle the data range and pivot table name so that I can reuse the code in another worksheet?

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts
    At runtime you could determine the number of pivottables in the workbook:
    x = 0
    For each wks in Activeworkbook
    x = x + wks.PivotTables.Count
    next
    x = x+1

    and then in your creation code use something like:
    TableName:="PivotTable" & x,

    For the source you could use something like:
    SourceData:=ActiveSheet.Range("a1").CurrentRegion,

    Steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •