VBA Pivot | Not Recognizing Headers

rachel06

Board Regular
Joined
Feb 3, 2016
Messages
174
Office Version
  1. 365
Platform
  1. Windows
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??

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
 

Forum statistics

Threads
1,226,812
Messages
6,193,116
Members
453,777
Latest member
Miceal Powell

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