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
Related
I am writing a macro which creates several pivot tables. All but 1 table runs fine, but I am getting a runtime error code for the one below: unable to get the pivottables property of the worksheet class
Dim PT As Excel.PivotTable
Set PT = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"GFCID!R1C1:R117200C129", Version:=xlPivotTableVersion14).CreatePivotTable( _
TableDestination:="'source'!R1C1", TableName:="Banking Book Pivot", _
DefaultVersion:=xlPivotTableVersion14)
With PT.PivotFields( _
"IRU")
.Orientation = xlRowField
.Position = 1
End With
' ActiveSheet.PivotTables("Banking Book Pivot").AddDataField ActiveSheet.PivotTables( _
' "Banking Book Pivot").PivotFields("GFCID"), "Count of GFCID", xlCount
' PT.AddDataField ActiveSheet.PivotTables( _
' "Banking Book Pivot").PivotFields("GFCID"), "Count of GFCID", xlCount
' With ActiveSheet.PivotTables("Banking Book Pivot").PivotTables("Banking Book Pivot").PivotFields("GFCID" & Chr(10) & "Count")
' .Orientation = xlDataField
' .Function = xlCount
' .Name = "Count of GFCID"
' End With
I have tried each of the three codes which are commented out, and they all get the same error. The first two codes work with other pivot tables in the macro. The data set for this pivot table is much larger than the others, but I don't think that should make a difference.
Don't use ActiveSheet. Use PT:
With PT
.AddDataField .PivotFields("GFCID"), "Count of GFCID", xlCount
End With
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 have the following code. When it gets to the ActiveWorkbook.PivotCaches .. i get the invalid procedure call or argument. I built this using the Macro Recorder. Not sure how to fix it.
Thanks for the help.
Sub PivotTable()
Sheets("YTD Sr Director").Select
Range("A3").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"YTD Detail!R1C1:R1048576C19", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="YTD Sr Director!R3C1", TableName:= _
"PivotTable2", DefaultVersion:=xlPivotTableVersion14
Sheets("YTD Sr Director").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Pay Amount")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("# of Drivers"), "Count of # of Drivers", xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Safety Bonus Paid"), "Count of Safety Bonus Paid",_
xlCount
End Sub
You just need to enclose your sheet name with pair of single quote:
From "YTD Detail!R1C1:R1048576C19"
to "'YTD Detail'!R1C1:R1048576C19"
in the SourceData and TableDestination arguments.
For more info, read here:
http://www.ugrowit.net/0815-excel-pivot-error-while-recording-a-macro-to-create-pivot-table-automatically/
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
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.