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
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!!
My macro is supposed to create a Pivot Table and add specific filters.
I used the macro recorder for this, it works on my computer but not on newer versions of excel. Heres my code:
Cells.Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"1!R1C1:R1048576C13", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Tabelle2!R3C1", TableName:="PivotTable2", _
DefaultVersion:=xlPivotTableVersion14
Sheets("Tabelle2").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("PlatzID/Gerät")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Tagabfluss"), "Anzahl von Tagabfluss", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Anzahl von Tagabfluss" _
)
.Caption = "Summe von Tagabfluss"
.Function = xlSum
End With
I tried to change Version:=xlPivotTableVersion14 to Version:=xlPivotTableVersion15 and other numbers which are for other excel versions but none seem to work.
I am using Excel 2010 my coworkers are using Excel 2013.
The Debug function highlights:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"1!R1C1:R1048576C13", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Tabelle2!R3C1", TableName:="PivotTable2", _
DefaultVersion:=xlPivotTableVersion14
First off, you really shouldn't use entire columns for the source data for a pivot table. Second, breaking the code up into discrete sections will help you to narrow down whether the error is in creating the pivotcache, or the pivot table itself. I'd suggest using:
Dim pivotSheet As Worksheet
Set pivotSheet = Sheets.Add
With Sheets("1")
Dim pc As PivotCache
Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:="'" & .Name & "'!" & .Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1), _
Version:=xlPivotTableVersion14)
End With
Dim pt As PivotTable
Set pt = pc.CreatePivotTable(TableDestination:=pivotSheet.Cells(3, 1), DefaultVersion:=xlPivotTableVersion14)
With pt
With .PivotFields("PlatzID/Gerät")
.Orientation = xlRowField
.Position = 1
End With
.AddDataField .PivotFields("Tagabfluss"), "Summe von Tagabfluss", xlSum
End With
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 an issue with macros returning errors, and I was wondering if you guys could shed some light on this issue.
The macro I have looks like the following:
Sub pivot1_macro()
'
' pivot1_macro Macro
'
' Keyboard Shortcut: Ctrl+Shift+M
'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"7-14 week!R1C2:R199C35", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="Sheet5!R3C1", TableName:="PivotTable3", DefaultVersion _
:=xlPivotTableVersion15
Sheets("Sheet5").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("PartMOD")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("EnteredShift")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Defect")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Defect Quantity"), "Sum of Defect Quantity", xlSum
Columns("D:D").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Range("B3").Select
Range("B3").Select
End Sub
I'd like to apply this macro to any active worksheet, but I'm not quite sure
how to make set 'SourceData' to current active worksheet.
You can use '&' to concatenate strings, so wouldn't
SourceData := ActiveSheet.Name & "!R1C2:R199C35"
work for you to get the current sheet's name?
I have recorded a macro to create a pivot table and subsequent chart in VBA. It works like a charm, just not at all how I need it to. The problem is that I want to be able to run the code and have it create a table on a sheet that doesn't already exist. Basically I will run this from a button on a menu, and it should create the the table and chart on a new page. Without further adieu:
Sub Macro13()
' Macro13 Macro
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Main Budget Sheet!R2C1:R88C10", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="Sheet21!R1C1", TableName:= _
"PivotTable12", DefaultVersion:=xlPivotTableVersion14
Sheets("Sheet21").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable12").PivotFields("Category")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable12").AddDataField ActiveSheet.PivotTables( _
"PivotTable12").PivotFields("Labor"), "Sum of Labor", xlSum
ActiveSheet.PivotTables("PivotTable12").AddDataField ActiveSheet.PivotTables( _
"PivotTable12").PivotFields("Material"), "Sum of Material", xlSum
ActiveSheet.Shapes.AddChart.Select
End Sub
If I was putting this into plain English, I would assume that it means... Select Data in Main Budget Sheet then create a pivot table on sheet 21 named pivot table 12... Then Select sheet 21 and select the fields for the pivot table...Finally, add chart from that data...
I think what I need to do is just figure out general names for each of these things (besides source data).
Also, currently it will only run once and only if that sheet already exists, presumably because it can't make a pivot table named the same thing, so if it could repeatedly just create the same table on different sheets I could die happy.
As you can see, I've tried this enough that I have created 21 sheets :/ so the effort is there, but as Einstein definition of insanity states "Insanity is trying the same thing and expecting a different result." Currently, I'm running out of new things to try.
Add a new sheet then use that sheet to add the new pivot too as follow:
Sub AddPivotOnNewSheet()
Dim wsNew As Worksheet
Set wsNew = Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Main Budget Sheet!R2C1:R88C10", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:=wsNew.Name & "!R1C1", TableName:= _
"PivotTableName", DefaultVersion:=xlPivotTableVersion14
With ActiveSheet.PivotTables("PivotTableName").PivotFields("Category")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTableName").AddDataField ActiveSheet.PivotTables( _
"PivotTableName").PivotFields("Labor"), "Sum of Labor", xlSum
ActiveSheet.PivotTables("PivotTableName").AddDataField ActiveSheet.PivotTables( _
"PivotTableName").PivotFields("Material"), "Sum of Material", xlSum
wsNew.Shapes.AddChart.Select
End Sub
Sub pivotnewsheet()
Dim ws As Worksheet
Dim pc As PivotCache
Dim pt As PivotTable
'Creating Pivot cache
On Error Resume Next
Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, Cells(5, 1).CurrentRegion) 'select first cells of your data range, in my case its start with Row5 column1 so i put cells(5,1)
'Adding new worksheet
Set ws = Worksheets.Add
ws.Name = "Summary"
'Creating Pivot table
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=pc, Tabledestination:=Range("A3"))
' it will create pivot table on sheet named summary at range A3
pt.Name = "PivotTable"
'Setting Fields
With pt
with .PivotFields("DEPT_NAME")
.Orientation = xlRowField
.Position = 1
End With
'set column field
With .PivotFields("QTY")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
End With
With .PivotFields("TOTAL_RETAIL_VALUE")
.Orientation = xlDataField
.Function = xlAverage
.Position = 2
.NumberFormat = "0.00" 'format for two decimals
End With
With .PivotFields("TOTAL_LANDED_COST")
.Orientation = xlDataField
.Function = xlAverage
.NumberFormat = "0.00"
.Position = 3
End With
'select classic view pivot
With ActiveSheet.PivotTables("PivotTable")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
End With
End Sub