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!!
Related
What commands should I input in order not needing to amend physically the sheet number when I run the pivot table macro and sheet name?
Dim pivotsheetname As String
Dim datasheetname As String
datasheetname = ActiveSheet.Name
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Orders!R1C1:R51291C24", Version:=6).CreatePivotTable TableDestination:="", TableName:="PivotTable1", DefaultVersion:=6
Sheets("pivotsheetname").Select
Sheets("pivotsheetname").Name = datasheetname & "Pivot"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Country")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Sales"), "Sum of Sales", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Profit"), "Sum of Profit", xlSum
Range("C8").Select
Range("C18").Select
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add "Cost incurred", _
"=SUM(Sales )-SUM(Profit )", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("Cost incurred"). _
Orientation = xlDataField
Range("B4:C5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "$#,##0.00"
Range("D6").Select
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Shipping Cost"), "Sum of Shipping Cost", xlSum
Range("E8").Select
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add _
"% of shipping cost", "=SUM('Shipping Cost' )/SUM('Cost incurred' )", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("% of shipping cost"). _
Orientation = xlDataField
Range("F2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Style = "Percent"
Range("D7").Select
With ActiveSheet.PivotTables("PivotTable1")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
Range("E9").Select
Application.GenerateGetPivotData = True
Application.GenerateGetPivotData = False
Range("D5").Select
End Sub
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
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 tried to record macros of creating a pivot chart. When I run the recorde33d macros after deleting the old chart, an error is being generated. "invalid procedure call or argument'
The error highlights the first line. Can pivot charts be done by macros? Or it just doesn't work when macros recorded?
Sub Macro5()
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Warranty!R1C1:R1048576C52", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="Customer Pareto!R12C6", TableName:= _
"PivotTable4", DefaultVersion:=xlPivotTableVersion15
Sheets("Customer Pareto").Select
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("'Customer Pareto'!$F$12:$H$29")
ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
PivotTable.PivotFields("VEH_IDENT_NBR"), "Count of VEH_IDENT_NBR", xlCount
With ActiveChart.PivotLayout.PivotTable.PivotFields("Step")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveChart.PivotLayout.PivotTable.PivotFields("SA Status")
.Orientation = xlColumnField
.Position = 2
End With
With ActiveChart.PivotLayout.PivotTable.PivotFields("SA_Failure_Mode")
.Orientation = xlRowField
.Position = 1
End With
ActiveChart.ChartType = xlBarStacked
With ActiveSheet.PivotTables("PivotTable4").PivotFields("SA_Failure_Mode")
.PivotItems("").Visible = False
.PivotItems("(blank)").Visible = False
End With
ActiveSheet.ChartObjects("Chart 6").Activate
ActiveSheet.Shapes("Chart 6").IncrementLeft 102
ActiveSheet.Shapes("Chart 6").IncrementTop -87
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Step")
.PivotItems("").Visible = False
.PivotItems("(blank)").Visible = False
End With
Range("G13").Select
ActiveSheet.PivotTables("PivotTable4").PivotFields("Step").PivotItems( _
"Implementation").ShowDetail = False
Range("H13").Select
ActiveSheet.PivotTables("PivotTable4").PivotFields("Step").PivotItems( _
"Solution").ShowDetail = False
ActiveSheet.ChartObjects("Chart 6").Activate
End Sub
Modify and use this as per your requirement:
Key things:
1.TableDestination = should be a range with no pivot table.
2.Change: TableDestination = Sheet Name instead of demo and DefaultVersion = xlPivotTableVersion15 instead of 10
3.Before calling the below Sub, activesheet should be the sheet containing sourceData
Sub Make_PivotTable()
Debug.Print ActiveWorkbook.PivotCaches.Count
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
ActiveSheet.UsedRange).CreatePivotTable TableDestination:="demo!R1C1", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10
Sheets("demo").PivotTables("PivotTable1").AddFields RowFields:="Name"
Sheets("demo").PivotTables("PivotTable1").PivotFields("Age").Orientation = _
xlDataField
Debug.Print ActiveWorkbook.PivotCaches.Count
End Sub
' You can see MS KB link for office 2007 to get informed about your error.
'If this helps Mark as Answer and/upvote, otherwise comment what happened after implementation.
The issue is with spaces in destination file name. The workaround that I have developed is as follows:
Dim wsNew As Worksheet
Set wsNew = Sheets.Add(After:=Sheets(Sheets.Count))
wsNew.Name = "Your Sheet Name with Spaces"
Sheets("Where my table sits as source data").Select
Range("A2").Activate
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"MyTableName", Version:=6).CreatePivotTable TableDestination:= _
"'" & wsNew.Name & "'!R3C2", TableName:="MyNewPivotName", DefaultVersion:=6
Sheets("DestinationSheet").Select
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.