VBA Add Pivot Table Sheet Name

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
150
Office Version
  1. 365
Platform
  1. Windows
When I run this code, it creates a pivot table on a new sheet. How do I get it so the sheet is always called "HHH Pivot Table"? The sheet number is not always the same.

VBA Code:
'HHH Overtime Check
    Sheets("Combined Time").Select
    Dim rng2 As Range
    Dim sht2 As Worksheet
    Dim pTable2 As PivotTable
    Set rng2 = ActiveSheet.Cells(1, 1).CurrentRegion
    Set sht2 = ActiveWorkbook.Worksheets.Add
    Set pTable2 = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        rng2.Address, Version:=8).CreatePivotTable(TableDestination:= _
        sht2.Cells(1, 1), TableName:="PivotTable" & Format(Time, "hhmmss"))
    With pTable2
        With .PivotFields("DOB")
            .Orientation = xlPageField
        End With
        With .PivotFields("PR Code")
            .Orientation = xlPageField
            Dim P2 As PivotItem
                 .EnableMultiplePageItems = True
            For Each P2 In .PivotItems
                If P2 = "HHH" Then
                    P2.Visible = True
                Else
                    P2.Visible = False
                End If
            Next P2
        End With
        With .PivotFields("SSN")
            .Orientation = xlRowField
            .Subtotals(1) = False
        End With
        .PivotFields("Reg Hours").Orientation = xlDataField
    End With
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try naming the sheet with this added instruction in the shown position:
VBA Code:
    Set sht2 = ActiveWorkbook.Worksheets.Add
    sht2.Name = "HHH Pivot Table"                                             '+++ ADD THIS LINE
    Set pTable2 = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
 
Upvote 0
Solution

Forum statistics

Threads
1,224,811
Messages
6,181,082
Members
453,021
Latest member
Justyna P

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