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
Related
I've been trying to run the following macro to create a pivot table out of a range from another sheet of data and I canĀ“t seem to make it work.
Sub topc()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Worksheets("TOP C").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "TOP C"
Application.DisplayAlerts = True
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Complaints!R1C1:R3000C32", Version:=6).CreatePivotTable _
TableDestination:="TOP C!R1C1", TableName:="PivotTable1", _
DefaultVersion:=6
ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Recurrence")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Subtype Name")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Type Inquiry")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Value"), "Sum of Value", xlSum
End Sub
I keep getting error "Invalid Procedure or Argument" in this area:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Complaints!R1C1:R3000C32", Version:=6).CreatePivotTable _
TableDestination:="TOP C!R1C1", TableName:="PivotTable1", _
DefaultVersion:=6
and keeps telling me xlDatabase = 1, idk if thats the problem. PLEASE HELP!!
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.
I have some code I run each morning that converts transactional data into a table then creates a Pivot Table based off of that data.
I have a button in a separate spreadsheet that I use to run these macros, but when I use the button the code formats the dates in US format not UK.
Sub TUFRFormat()
Application.DisplayAlerts = False
Columns("A:Y").Select
Selection.Columns.AutoFit 'Ensures data is displayed correctly
Columns("B:B").Select 'Rearanges columns to be presented correctly
Selection.Cut Destination:=Columns("Z:Z")
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft
Range("F:F,G:G,H:H,I:I,J:J,U:U,V:V,W:W,X:X").Select
Selection.EntireColumn.Hidden = True 'Hides unrelevant columns
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$Y$50000"), , xlYes).Name _
= "Table1"
Range("Table1[#All]").Select
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight9" 'Selects a table theme
Range("Table1[[#Headers],[Unit]]").Select
ChDir "P:\Desktop\Prior Day Journals\Tueday - Friday"
' Creates a pivot table based off of the sheet 'Data'
Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Table" 'Adds new sheet and names it table
Sheets("Table").Select
Sheets("Table").Move Before:=Sheets(1) 'Moves to the first sheet in the document
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
:="Table!R1C1", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion14
Sheets("Table").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Posted")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Date")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Year")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Period")
.Orientation = xlColumnField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Unit")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Sum Amount3"), "Count of Sum Amount3", xlCount
ActiveSheet.PivotTables("PivotTable1").RowAxisLayout xlOutlineRow
ActiveSheet.PivotTables("PivotTable1").PivotFields("Posted").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Posted")
.PivotItems("(blank)").Visible = False
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("Posted"). _
EnableMultiplePageItems = True
I have tried looking for an obvious answer, but when I run the code directly rather than through the button it formats the dates as the UK format.
Any help would be appreciated
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
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