This macro is one I got offline and it work perfectly with another tab, but once I try and use a second tab with different data I keep getting an error, It might be a simple fix that I am overlooking if not underlined the spot in the code that brings back the error.
Sub Macro1()
Dim pt As PivotTable
Dim strField As String
Dim WSD As Worksheet
Set WSD = Worksheets("aa")
Dim PTOutput As Worksheet
Sheets.Add.Name = "bb"
Set PTOutput = Worksheets("bb")
Dim PTCache As PivotCache
Dim PRange As Range
Sheets("aa").Select
' Find the last row with data
Dim finalRow As Long
finalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
' Find the last column with data
Dim finalCol As Long
finalCol = WSD.Cells(1, Application.Columns.Count).End(xlToLeft).Column
' Find the range of the data
ActiveWorkbook.PivotCache.MissingItemsLimit = xlMissingItemsNone
Set PRange = WSD.Cells(1, 1).Resize(finalRow, finalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange):mad:
' Create the pivot table
Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutput.Cells(1, 1), TableName:="SamplePivot3")
' Define the layout of the pivot table
' Set update to manual to avoid recomputation while laying outpt
pt.ManualUpdate = True
' Set up the row fields
pt.AddFields RowFields:=Array("ItemNumber")
thanks for the help
' Set up the data fields
With pt.PivotFields("ReceiptQty")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
End With
' Now calc the pivot table
pt.ManualUpdate = False
End Sub
Bookmarks