I have the following code...
Sub CreateAPivotTable()
Dim shtSource As Worksheet
Dim rngSource As Range
Dim rngDest As Range
Dim pvt As PivotTable
On Error GoTo ErrHandler
'this prevents the screen from updating while the macro is running and
'will make the code run faster
Application.ScreenUpdating = False
Set shtSource = ActiveSheet
If shtSource.Name <> "Summary" Then
'Rather than have the pivot table use all rows in column A-N
'just use what has actually been used.
Set rngSource = shtSource.Range("A1").CurrentRegion
'This is where the pivot table will be placed
Set rngDest = shtSource.Range("E1")
'This creates a pivot table. So rather than having to refer to PivotTables("PivotTable14") like before you can just refer to pvt
Set pvt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSource, _
Version:=xlPivotTableVersion12).CreatePivotTable(TableDestination:=rngDest, _
DefaultVersion:=xlPivotTableVersion12)
pvt.AddDataField pvt.PivotFields("Serial Rcvd"), "Count of Serial Rcvd", xlCount
pvt.AddDataField pvt.PivotFields("Serial Shipped"), "Count of Serial Shipped", xlCount
pvt.PivotFields("Count of Serial Rcvd").Caption = " Serial Rcvd"
pvt.PivotFields("Count of Serial Shipped").Caption = " Serial Shipped"
With pvt.PivotFields("Item")
.Orientation = xlRowField
.Position = 1
End With
'Formatting
pvt.TableStyle2 = "PivotStyleDark7"
With shtSource.Cells.Font
.Name = "Calibri"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
ActiveWorkbook.ShowPivotTableFieldList = False
Else
MsgBox ("Please remove existing sheet with the name of Summary")
End If
Application.ScreenUpdating = True
Exit Sub
'Simple error handler in case something goes wrong
ErrHandler:
Application.ScreenUpdating = True
MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"
End Sub
It works great on the current worksheet it is tied to, but how do I apply this code to an identical worksheet, that needs the same pivot tables created? I want to me able to basically..."Copy & Paste" this code to any workbook of similar data. HELP?!?!?!?!!?!?!?!?!![]()
Bookmarks