Pivot table with a changing worksheet name

Shen1986

New Member
Joined
Apr 13, 2021
Messages
5
Office Version
  1. 2016
Hi

I am new here and fresh to this. However I have a question and have been surfing the net and found a answer but it does not work:


I have recorded the macro to create a pivot table as a basis but I have a problem that the worksheet name which is the basis for the pivot table changes every date. Is there a way besides that one in the video how it could work because when I have tried that solution it and still it does not work.

Thank you very much for reading this and your reply in advance.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
When you say it doesn't work what do you mean ?
The assumption is that the worksheet you want to create the pivot for is the Activesheet and that it will create the new Pivot on a new sheet. Also that the headings of fields that are used in the pivot on each sheet are exactly the same

If its erroring out click the Debug button and show us which line its erroring out on.

Can you show us the code you have. And also an XL2BB shot of the Source Sheet being used for the pivot when it fails, we only need a sample of the rows but need the copy to start at A1 and show all the columns.
 
Upvote 0
Hi

Thanks a lot for your reply. I am actually trying to achieve that the source data would not be hard wired into the name but it would be dynamic because that source changes daily the review file is generated differently every day and I would need to create a macro that would create the pivot table in one click. I tried the thing in the video but that did not work.

Range("A1:AE2").Select
Range("AE2").Activate
Selection.Delete Shift:=xlUp
Range("A1:AE16").Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
[Review29321"!R1C1:R16C31"], Version:=6).CreatePivotTable _
TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion _
:=6
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1")
.ColumnGrand = True
.HasAutoFormat = True
.DisplayErrorString = False
.DisplayNullString = True
.EnableDrilldown = True
.ErrorString = ""
.MergeLabels = False
.NullString = ""
.PageFieldOrder = 2
.PageFieldWrapCount = 0
.PreserveFormatting = True
.RowGrand = True
.SaveData = True
.PrintTitles = False
.RepeatItemsOnEachPrintedPage = True
.TotalsAnnotation = False
.CompactRowIndent = 1
.InGridDropZones = False
.DisplayFieldCaptions = True
.DisplayMemberPropertyTooltips = False
.DisplayContextTooltips = True
.ShowDrillIndicators = True
.PrintDrillIndicators = False
.AllowMultipleFilters = False
.SortUsingCustomLists = True
.FieldListSortAscending = False
.ShowValuesRow = False
.CalculatedMembersInFilters = False
.RowAxisLayout xlCompactRow
End With

Thanks a lot for reading this and have a nice day.
 
Upvote 0
I can't see the changes in the video reflected in the code you sent me, so I am not sure what was not working for you ?
In the code you have sent me, I don't know what the first 4 lines are supposed to do so I have commented it out.
Also what you have recorded only sets up the "empty" pivot table.

The macro below is mostly following what was in the Video you referred to.
I have made a few changes.

The sheet you want that has the data in it to use as the basis of the pivot has to be the active sheet when you run the code.
The code also currently assumes your data range starts in A1 and has at least 3 columns of data in it.


VBA Code:
Sub RecordedMacroModified()

    Dim lr As Long
    Dim lc As Long
    Dim dataWSName As String
    Dim pivotWSName As String
    Dim pivotWS As Worksheet
    Dim dataRng As Range
        
    dataWSName = ActiveSheet.Name
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    
    If lr < 2 Or lc < 3 Then
        MsgBox "Must have data for a Pivot Table"
        Exit Sub
    End If
    
    With Worksheets(dataWSName)
        Set dataRng = .Range(.Cells(1, 1), .Cells(lr, lc))
    End With
    
    ' Additional undocumented steps recorded by user
    'Range("A1:AE2").Select
    'Range("AE2").Activate
    'Selection.Delete Shift:=xlUp
    'Range("A1:AE16").Select

    Sheets.Add
    Set pivotWS = ActiveSheet
    pivotWSName = pivotWS.Name
        
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    dataRng, Version:=6).CreatePivotTable _
    TableDestination:=pivotWS.Range("A3"), TableName:="PivotTable1", DefaultVersion _
    :=6
    
    Sheets(pivotWSName).Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable1")
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
    End With


End Sub
 
Upvote 0
I can't see the changes in the video reflected in the code you sent me, so I am not sure what was not working for you ?
In the code you have sent me, I don't know what the first 4 lines are supposed to do so I have commented it out.
Also what you have recorded only sets up the "empty" pivot table.

The macro below is mostly following what was in the Video you referred to.
I have made a few changes.

The sheet you want that has the data in it to use as the basis of the pivot has to be the active sheet when you run the code.
The code also currently assumes your data range starts in A1 and has at least 3 columns of data in it.


VBA Code:
Sub RecordedMacroModified()

    Dim lr As Long
    Dim lc As Long
    Dim dataWSName As String
    Dim pivotWSName As String
    Dim pivotWS As Worksheet
    Dim dataRng As Range
       
    dataWSName = ActiveSheet.Name
   
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
   
    If lr < 2 Or lc < 3 Then
        MsgBox "Must have data for a Pivot Table"
        Exit Sub
    End If
   
    With Worksheets(dataWSName)
        Set dataRng = .Range(.Cells(1, 1), .Cells(lr, lc))
    End With
   
    ' Additional undocumented steps recorded by user
    'Range("A1:AE2").Select
    'Range("AE2").Activate
    'Selection.Delete Shift:=xlUp
    'Range("A1:AE16").Select

    Sheets.Add
    Set pivotWS = ActiveSheet
    pivotWSName = pivotWS.Name
       
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    dataRng, Version:=6).CreatePivotTable _
    TableDestination:=pivotWS.Range("A3"), TableName:="PivotTable1", DefaultVersion _
    :=6
   
    Sheets(pivotWSName).Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable1")
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
    End With


End Sub
Thanks a lot it worked.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top