+ Reply to Thread
Results 1 to 3 of 3

Dynamically use sheet names for pivot tables.

Hybrid View

kleptilian Dynamically use sheet names... 01-12-2016, 02:04 PM
dflak Re: Dynamically use sheet... 01-13-2016, 01:13 PM
kleptilian Re: Dynamically use sheet... 01-14-2016, 08:59 AM
  1. #1
    Forum Contributor
    Join Date
    03-14-2013
    Location
    FL
    MS-Off Ver
    Excel 2013
    Posts
    123

    Dynamically use sheet names for pivot tables.

    Hello,

    I'm currently working on a macro that will loop through my sheets and create pivot tables for each sheet. I have the pivot macros down now I'm having trouble getting it to work across all sheets dynamically. The below code works for one sheet that I pre-defined.
    How would I create the Pivot table so that it would be dynamic. I want the macro to create the source data based off the worksheet that it's pulling from. Also i want the destination sheet to be named the same as the source sheet, except to add pivot at the end. so sheet 1 would also have sheet 1 pivot that would contain the pivot table.
    Basically how do I write the code so that the yellow sections are set to be dynamic and based off the sheet that it is creating the pivot table from?

    If i have a loop
    Sub macro()
    Dim ws As Worksheet
    For Each ws In Worksheets
    
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
            ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "ALUGA!R1C1:R1048576C27", Version:=xlPivotTableVersion15).CreatePivotTable _
            TableDestination:="'ALUGA Pivot'!R1C1", TableName:="PivotTable1", _
            DefaultVersion:=xlPivotTableVersion15
    
    Next ws
    
        End Sub

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Dynamically use sheet names for pivot tables.

    Since I don't have a workbook this is "air code." Assuming the rest of your code works, this should work. Also the code as you have it will try to create multiple PivotTable1. This might cause a problem. I took care of that with a counter. You may want to name the pivot table something more descriptive.

    Sub macro()
    Dim ws As Worksheet
    Dim SheetName As String
    Dim PTNumber As Long
    
    PTNumber = 0
    
    For Each ws In Worksheets
    SheetName = ws.Name
    PTNumber = PTNumber + 1
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
            ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "'" & SheetName & "!R1C1:R1048576C27", Version:=xlPivotTableVersion15).CreatePivotTable _
            TableDestination:="'" & SheetName & " Pivot'!R1C1", TableName:="PivotTable" & PTNumber, _
            DefaultVersion:=xlPivotTableVersion15
    
    Next ws
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    03-14-2013
    Location
    FL
    MS-Off Ver
    Excel 2013
    Posts
    123

    Re: Dynamically use sheet names for pivot tables.

    I found through Googling the below code and it works! Thanks for your help regardless.
    Sub Macro1()
    
        Dim shtSrc As Worksheet, shtDest As Worksheet
        Dim pc As PivotCache
    
        Set shtSrc = ActiveSheet
    
        Set shtDest = shtSrc.Parent.Sheets.Add()
        shtDest.Name = shtSrc.Name & "-Pivot"
    
        Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
            SourceData:=shtSrc.Range("A1").CurrentRegion)
        pc.CreatePivotTable TableDestination:=shtDest.Range("A3"), _
            TableName:="PivotTable1"
    End Sub

+ 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. Dynamically changing several pivot tables at once
    By Jason in forum Excel General
    Replies: 4
    Last Post: 10-18-2021, 05:59 PM
  2. Field names in Pivot Tables
    By ellenhotz in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 06-13-2013, 09:54 PM
  3. Replies: 3
    Last Post: 04-17-2011, 03:23 AM
  4. Updating Pivot Tables Dynamically
    By JagR in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-11-2010, 04:56 AM
  5. [SOLVED] Dynamically changing print areas for Pivot Tables
    By Todd1 in forum Excel General
    Replies: 1
    Last Post: 08-16-2006, 10:00 PM
  6. range names in pivot tables
    By Forrest in forum Excel General
    Replies: 0
    Last Post: 01-10-2006, 02:45 PM
  7. Names into Pivot Tables
    By fishy8000 in forum Excel General
    Replies: 0
    Last Post: 03-24-2005, 03:30 PM
  8. Pivot Tables and Names
    By fishy8000 in forum Excel General
    Replies: 0
    Last Post: 03-24-2005, 03:09 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