Hi,
I'm working on something that moves data to a new sheet and pivots. Right now, I'm getting a weird result where it's using the first row (Pivot Fields) the header, but also the first row of data. Is there something I need to add to this??
Note that this is just a piece of the code
I'm working on something that moves data to a new sheet and pivots. Right now, I'm getting a weird result where it's using the first row (Pivot Fields) the header, but also the first row of data. Is there something I need to add to this??
Code:
Sub PivotRpt()
Dim PTable As PivotTable
Dim PCache As PivotCache
Dim PRange As Range
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim LR As Long
Dim LC As Long
Dim pField As PivotField
Dim ws As Worksheet
Dim FPath As String
On Error Resume Next
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set ws = Worksheets("Rpt1")
ws.Copy
Worksheets.Add After:=ActiveSheet ' This will add new worksheet
ActiveSheet.Name = "Pivot" ' This will rename the worksheet as "Pivot Sheet"
On Error GoTo 0
Set PSheet = Worksheets("Pivot")
Set DSheet = Worksheets("Rpt1")
'Find Last used row and column in data sheet
LR = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LC = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
'Set the pivot table data range
Set PRange = DSheet.Cells(1, 1).Resize(LR, LC)
'Set pivot cahe
Set PCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, SourceData:=PRange)
'Create blank pivot table
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), TableName:="Pivot")
'Insert Row Fields
With PSheet.PivotTables("Pivot").PivotFields("Payee ID")
.Orientation = xlRowField
.Position = 1
End With
With PSheet.PivotTables("Pivot").PivotFields("Carrier ID")
.Orientation = xlRowField
.Position = 2
End With
With PSheet.PivotTables("Pivot").PivotFields("Rebate Qtr End Date")
.Orientation = xlRowField
.Position = 3
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Note that this is just a piece of the code