Pivot Table Error 1004 when adding Fields - excel

I have this macro that creates a pivot table from a ranged data ("PvtData").
I keep getting run time error 1004 (Application defined or Object defined error) when the code is trying to add PivotFields.
Below is a portion of the code;
Workbooks(PivotFile).PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"PvtData", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="PvtResult", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
'Work up to here
With ActiveSheet.PivotTables("PivotTable1").PivotFields("COB")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Product")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Book")
.Orientation = xlRowField
.Position = 2
End With
I've tried recording the macro, which gives the same code line.
Changing the table destination to empty ("") does not help.

After adding the fields try your piece of code as it is.
pvtTable.AddFields("COB")
With pvtTable.PivotFields("COB")
.Orientation = xlColumnField
.Position = 1
End With

Related

Value Field not Displaying Values when using VBA to make a pivot table

The code does not cause an error and a table with the correct row and column fields are created. Nothing appears in the value field though.
'Insert Row Fields
With ActiveSheet.PivotTables("Rev by Facility")
With .PivotFields("Facility")
.Orientation = xlRowField
.Position = 1
End With
'Insert Column Fields
With .PivotFields("Revenue")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
With .PivotFields("SubTotal 2")
.Orientation = xlValuesField
.Position = 1
.Function = xlSum
.Name = "Sum of SubTotal 2"
End With
End With
This is only part of the code, the above code should not cause the value field to not appear. The data source of the pivot table includes the column and all the data I want to appear in the value field.
Not having your lines properly indented can cause all sorts of issues. Try this
'Insert Row Fields
With ActiveSheet.PivotTables("Rev by Facility")
With .PivotFields("Facility")
.Orientation = xlRowField
.Position = 1
End With
'Insert Column Fields
With .PivotFields("Revenue")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
With .PivotFields("SubTotal 2")
.Orientation = xlValuesField
.Position = 1
.Function = xlSum
.Name = "Sum of SubTotal 2"
End With
End With
To define a pivot field as "value field", you have to use xlDataField.
.Orientation = xlDataField
Just in case:
The often called "filter fields" have the orientation xlPageField.

Excel VBA adding Values field in Pivot Table not working

Hi I am trying to add a Values field in my Pivot table. Here is the code I have for now :
'Insert Row Fields
With ActiveSheet.PivotTables("PivotTable").PivotFields("Account Lvl1 Desc")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Account Description")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Supplier")
.Orientation = xlRowField
.Position = 3
End With
'Insert Data Field
With ActiveSheet.PivotTables("PivotTable")
.PivotFields ("Distrib Amt CAD")
.Orientation = xlDataField
.Function = xlSum
'.NumberFormat = "#,##0"
.Name = "Sum of OH in CAD "
End With
Everything else is working but the Values
I would recommend some clean-up:
With ActiveSheet.PivotTables("PivotTable")
'Insert Row Fields
With .PivotFields("Account Lvl1 Desc")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Account Description")
.Orientation = xlRowField
.Position = 2
End With
With .PivotFields("Supplier")
.Orientation = xlRowField
.Position = 3
End With
'Insert Data Field
With .PivotFields ("Distrib Amt CAD")
.Orientation = xlDataField
.Function = xlSum
'.NumberFormat = "#,##0"
.Name = "Sum of OH in CAD "
End With
End With
If you have values that you, say, exported from SharePoint field tables, those are strings... run a similar code to the below on your data so you can display as a number:
With sws 'source worksheet
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To lr
.Cells(i, 6).Value = Val(.Cells(i, 6).Value) * 1 'Uses column 6 (F)
Next i
End With
Once the appropriate data exists, it will give more than #Value errors on the output.

Excel macro for creating pivot table

This is the recorded macro for creating a pivot table.
While running it I getting an error of:
error 1004 unable to get the pivot tables property of worksheet class
Range("_RallyQueryResultList[#All]").Select
Sheets.Add
ActiveWorkbook.Worksheets("Sheet30").PivotTables("PivotTable8").PivotCache. _
CreatePivotTable TableDestination:="Sheet30!R3C1", TableName:="PivotTable9" _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Sheet30").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable9").PivotFields("Severity")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable9").AddDataField ActiveSheet.PivotTables( _
"PivotTable9").PivotFields("Severity"), "Count of Severity", xlCount
With ActiveSheet.PivotTables("PivotTable9").PivotFields("Owner")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable9").PivotFields("Requirement")
.Orientation = xlPageField
.Position = 1
End With
End Sub

Type mismatch error when creating a pivot table in Excel with VBA

I'm trying to put a macro together that will make a simple pivot table using the data from an active worksheet. When I try to run it, I receive a type mismatch error. When I start the debugger, the first section is highlighted: ActiveWorkbook.PivotCaches through xlPivotTableVersion10. Initially, the TableDestination was blank and I thought that might be the problem, but after adding a destination I still get the same error.
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=ActiveSheet.UsedRange).CreatePivotTable TableDestination:="Sheet1!R3C1", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(1, 1)
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Program Name")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Dollars Awarded"), "Sum of Dollars Awarded", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Dept Head")
.Orientation = xlRowField
.Position = 1
End With
This worked for me (XL2007):
Sub Tester()
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=ActiveSheet.UsedRange)
.CreatePivotTable TableDestination:="Sheet1!R3C1", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
End With
With Sheet1.PivotTables("PivotTable1")
.PivotFields("Dept Head").Orientation = xlColumnField
.PivotFields("Program Name").Orientation = xlRowField
.AddDataField .PivotFields("Cost"), "Sum of cost", xlSum
End With
End Sub
Make sure you don't already have an existing conflicting pivot cache/table.

Create Pivot Table

I got this Run-time error '1004':- This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following: If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again. If you're creating a PivotTable report or PivotChart report, type a [it stops here]
I got the aforementioned error at:-
pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12
I'm trying to run this code:-
Sub CreateTable()
Dim lastRow As Long
Dim pvc As PivotCache
lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow
Set pvc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Database", Version:=xlPivotTableVersion12)
pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12
Sheets("TABLE").Select
Cells(1, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("GRADE"), "Sum of GRADE", xlSum
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("SIZE"), "Sum of SIZE", xlSum
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("QTY"), "Sum of QTY", xlSum
With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
.Orientation = xlColumnField
.Position = 2
End With
With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
.Orientation = xlColumnField
.Position = 3
End With
With ActiveSheet.PivotTables("INFO").PivotFields("Sum of GRADE")
.Orientation = xlPINFOField
.Position = 1
End With
With ActiveSheet.PivotTables("INFO").PivotFields("Sum of SIZE")
.Orientation = xlRowField
.Position = 1
End With
ActiveWorkbook.ShowPivotTableFieldList = False
Range("B3").Select
ActiveSheet.PivotTables("INFO").CompactLayoutColumnHeader = "MODEL"
Range("A5").Select
ActiveSheet.PivotTables("INFO").CompactLayoutRowHeader = "SIZE"
Range("A1").Select
ActiveSheet.PivotTables("INFO").PivotFields("GRADE").Caption = "GRADE"
Cells.Select
Cells.EntireColumn.AutoFit
Columns("B:BB").Select
Selection.Style = "Comma"
Cells.Select
Cells.EntireColumn.AutoFit
Range("C1").Select
End Sub
Pls help.
Thanks.
It appears the reason that this error occurs is due to the fourth row:
lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
by the look of this, you are finding the last row of data in your "DATA" sheet by looking in column 6. Now if we look what you are combining this with:
ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow
Now, if your source data is in cells G1:K** then:
ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
will always return 1 unless you have data in column 6, but from the line above, it seems that your data starts in column 7 ("G"). Therefore, to get this to work you have to change the fourth row to:
lastRow = ActiveSheet.Cells(Rows.Count, 7).End(xlUp).Row

Resources