Pivot Tables in VBA: Problem with AddFields Method

J

jimec74

Hi

I seem to be having a problem adding fields to my Pivot Table in VBA. When
I reach the line: "ActiveSheet.PivotTables("RepData").AddFields..." below, I
get a Run Time Errorr (1004) stating "AddFields method of PivotTable class
failed".

Can someone please point me in the right direction?

Thanks,

James

===================================

Sub RunDailyReport(ByVal ShiftDate As Date, ByVal Shift As String)

'Clear existing PivotTable data:
Sheets("Rep_Daily").Activate
Rows("73:73").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents

'Create a new Pivot Table:
Sheets("All_data").Activate
Cells.Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"All_Data!C1:C24").CreatePivotTable TableDestination:= _
"'Rep_Daily'!R76C2", TableName:="RepData" _
, DefaultVersion:=xlPivotTableVersion10
Sheets("Rep_Daily").Activate
ActiveSheet.PivotTables("RepData").AddFields RowFields:="LoadedBy1", _
ColumnFields:=Array("Material1", "PlantID"),
PageFields:=Array("Date", "Shift")
With ActiveSheet.PivotTables("RepData").PivotFields("Loads1")
.Orientation = xlDataField
.Caption = "Sum of Loads1"
.Function = xlSum
End With
ActiveWorkbook.ShowPivotTableFieldList = True
ActiveSheet.PivotTables("RepData").PivotFields("Date").CurrentPage =
ShiftDate
Select Case Shift
Case "D"

ActiveSheet.PivotTables("RepData").PivotFields("Shift").CurrentPage = "D"
Case "N"

ActiveSheet.PivotTables("RepData").PivotFields("Shift").CurrentPage = "N"
Case "Both"

ActiveSheet.PivotTables("RepData").PivotFields("Shift").CurrentPage = "(All)"
End Select
With ActiveSheet.PivotTables("RepData").PivotFields("PlantID")
.PivotItems("EX8001").Visible = False
.PivotItems("(blank)").Visible = False
End With

End Sub

====================================
 
D

Debra Dalgleish

Your source data range isn't right: All_Data!C1:C24

Change that range so it includes all your headings and data rows, and
the code may work correctly.
 
J

jimec74

Thanks Debra

That got it working! Seems like a simple error in retrospect...

Thanks again,

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top