application defined or object defined error when creating calculated fields - vba

ActiveSheet.PivotTables("PivotTable13").AddDataField ActiveSheet.PivotTables( _
"PivotTable13").PivotFields("OOS VALUE"), "Sum of OOS VALUE", xlSum
ActiveSheet.PivotTables("PivotTable13").AddDataField ActiveSheet.PivotTables( _
"PivotTable13").PivotFields("Sales"), "Sum of Sales", xlSum
ActiveSheet.PivotTables("PivotTable13").CalculatedFields.Add "OOS", _
"=OOS VALUE/Sales", True
ActiveSheet.PivotTables("PivotTable13").PivotFields("OOS").Orientation = _
xlDataField
With ActiveSheet.PivotTables("PivotTable13").PivotFields("Sum of OOS VALUE")
.NumberFormat = "?#,##0.00"
End With
With ActiveSheet.PivotTables("PivotTable13").PivotFields("Sum of Sales")
.NumberFormat = "?#,##0.00"
End With
With ActiveSheet.PivotTables("PivotTable13").PivotFields("Sum of OOS")
.NumberFormat = "0.00%"
End With
I am getting the application defined or object defined error at the part where the calculated field is made. what's wrong? i'm using the record a macro feature if that helps

Related

Runtime error when adding more than 1 pivot table to existing worksheet

I have a macro that creates a new worksheet and pivot table (which works just fine). As soon as I try to add a second pivot table to this worksheet, I get this error message.
Error message: Run-time error '5': Invalid procedure call or argument
When I create the second pivot in a new worksheet, it works just fine, so I think the issue is when I select add to existing worksheet.
Here is the code for the second pivot (created using a macro, sorry I'm new to this so it may be a bit messy), error is on line 3. Not sure why it references the pivot table "Calls by Region/Analyst", which is the already existing pivot:
Sheets("Sorted Data").Select
ActiveCell.Offset(1, -7).Range("Table1[[#Headers],[Status]]").Select
ActiveWorkbook.Worksheets("Pivot Tables").pivotTables("Calls by Region/Analyst" _
).PivotCache.CreatePivotTable TableDestination:="Pivot Tables!R3C6", _
TableName:="PivotTable6", DefaultVersion:=xlPivotTableVersion12
Sheets("Pivot Tables").Select
Cells(3, 6).Select
ActiveSheet.pivotTables("PivotTable6").Name = "Analyst"
ActiveSheet.pivotTables("Analyst").AddDataField ActiveSheet.pivotTables( _
"Analyst").PivotFields("Assigned User Name"), "Count of Assigned User Name", _
xlCount
ActiveSheet.pivotTables("Analyst").PivotFields("Count of Assigned User Name"). _
Caption = "Number of Calls"
With ActiveSheet.pivotTables("Analyst").PivotFields("Assigned User Name")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.pivotTables("Analyst").CompactLayoutRowHeader = "Analyst"
ActiveCell.Offset(0, 4).Range("A1").Select
End Sub
Here's the code for the second pivot that works when opening it in a new worksheet, however I need it in an existing worksheet (at F3):
Sheets("Sorted Data").Select
ActiveCell.Offset(1, -7).Range("Table1[[#Headers],[Status]]").Select
Sheets.Add
ActiveWorkbook.Worksheets("Pivot Tables").pivotTables("Calls by Region/Analyst" _
).PivotCache.CreatePivotTable TableDestination:="Sheet3!R3C1", TableName:= _
"PivotTable5", DefaultVersion:=xlPivotTableVersion12
Sheets("Sheet3").Select
Cells(3, 1).Select
ActiveSheet.pivotTables("PivotTable5").Name = "Analyst"
ActiveSheet.pivotTables("Analyst").AddDataField ActiveSheet.pivotTables( _
"Analyst").PivotFields("Assigned User Name"), "Count of Assigned User Name", _
xlCount
ActiveSheet.pivotTables("Analyst").PivotFields("Count of Assigned User Name"). _
Caption = "Number of Calls"
With ActiveSheet.pivotTables("Analyst").PivotFields("Assigned User Name")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.pivotTables("Analyst").CompactLayoutRowHeader = "Analyst"
ActiveCell.Offset(1, 0).Range("A1").Select

How to make my pivot fields dynamic in vba code

I have a pivot table that sorts data for various people for each month over a 12-month period. The table that the data is generated from has a dynamic date range (ie the user can specify the start date which will cascade through the table). The pivot table and chart is generated from this master table as part of the data analysis.
This worked fantastic last month when I created this tool, however I realised in my code I have hard-coded names of the pivot fields to correspond to months from April (please see below). This makes my pivot table break when I try to run it from a start date in May. How can I make the "PivotFields"/data fields in the pivot table dynamic to correspond to the date range?
Relevant piece of the code found below. As one would expect, the error happens at:
"Compare individuals").PivotFields("Apr-17"), "Sum of Apr-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField
When trying to run my pivot chart/report from May.
Really appreciate any pointers!
Option Explicit
Sub CreatePivot()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim pc As PivotCache
With ActiveWorkbook
For Each pc In .PivotCaches
pc.MissingItemsLimit = xlMissingItemsNone
Next pc
End With
'Generate base pivot chart
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Indy", Version:=xlPivotTableVersion10). _
CreatePivotTable TableDestination:="'PivotSheet'!R8C6", TableName:="Compare individuals" _
, DefaultVersion:=xlPivotTableVersion10
Worksheets("PivotSheet").Activate
'Set Name and disc as field and filter respectively
With ActiveSheet.PivotTables("Compare individuals").PivotFields("Name")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Compare individuals").PivotFields("Disc ")
.Orientation = xlPageField
.Position = 1
End With
'Add data; sums of each month per individual
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Apr-17"), "Sum of Apr-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("May-17"), "Sum of May-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Jun-17"), "Sum of Jun-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Jul-17"), "Sum of Jul-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Aug-17"), "Sum of Aug-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Sep-17"), "Sum of Sep-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Oct-17"), "Sum of Oct-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Nov-17"), "Sum of Nov-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Dec-17"), "Sum of Dec-17", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Jan-18"), "Sum of Jan-18", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Feb-18"), "Sum of Feb-18", xlSum
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields("Mar-18"), "Sum of Mar-18", xlSum
ActiveSheet.PivotTables("Compare individuals").PivotFields("Disc ").CurrentPage = _
"(All)"
'Set data in columns
With ActiveSheet.PivotTables("Compare individuals").DataPivotField
.Orientation = xlColumnField
.Position = 1
End With
EDIT: a bit more context.
My PivotTable looks like this:
The error ostensibly shows up because there's no "Apr-17" in the actual data table
My source data looks like this:
Wherein the months/date range are dynamic (from initial input).
What I want the code to do is this:
Ie put all the date columns into the table regardless of what they are called.
#A.Doe, give this a try
Dim f1 As PivotField
For Each f1 In ActiveSheet.PivotTables("Compare individuals").PivotFields
'MsgBox f1.Name test code
If (InStr(f1.Name, "17") > 0) Then 'or some valid condition
ActiveSheet.PivotTables("Compare individuals").AddDataField ActiveSheet.PivotTables( _
"Compare individuals").PivotFields(f1.Name), "Sum of " & f1.Name, xlSum
End If
Next

transform Vertical information into horizontal

I would like to have this transformation in vba
And to inderstend better i shere with you this picture
Now i have another column and i should have this information in the new table, but i don't find haw to make this?
All I did was setup your sample data and then record a macro and saved recorded macro generated below.
Now note: if your data is larger then you may want to record using key strokes to put cursor at beginning of table and then at the end of the table or "Define data as a table" and use the table.... but this gives you the general idea.
Sub Macro1()
' Macro1 Macro
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R10C4", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet1!R13C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(13, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Customer ")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Group"), "Sum of Group", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Price")
.Orientation = xlColumnField
.Position = 1
End With
Columns("C:C").ColumnWidth = 5.71
ActiveSheet.PivotTables("PivotTable1").PivotFields("Price").Subtotals = Array( _
False, True, False, False, False, False, False, False, False, False, False, False)
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Group")
.Orientation = xlColumnField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Price"), "Sum of Price", xlSum
Range("E14").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Price").Caption = _
"Prix"
Range("B18").Select
ActiveSheet.PivotTables("PivotTable1").RowGrand = False
End Sub
Which then results in:
to handle the new column: Id combine the values in the raw data. if however you are after something different I would need to see an example of the desired output.

Creating a pivot table in VBA

I have an xlsm file with some VBA code in it. I basically have a folder with a lot of csv files in it. When I execute the VBA code in the xlsm file, it needs to loop through each csv file, select the data in it and then create a pivot table from the data and export the sheet with the pivot table into a fresh workbook.
It needs to be dynamic in the sense that the amount of data (rows) is not the same in each csv file, so it first needs to select the block of data and then create the pivotcaches etc.
My code fails when trying to create the pivot table. See below for the relevant code. This code kicks off the job to loop through each csv:
Sub RunBatch()
Dim Filename, Pathname As String
Dim wb As Workbook
Pathname = "\\troy\Anfield\Product & Risk Management\Muhammad\2014\PnL Attribution Reports\20140822\"
Filename = Dir(Pathname & "*.csv")
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
CreatePivotTableSummary wb
wb.Close SaveChanges:=True
Filename = Dir()
Loop
End Sub
This code is the one that actually creates the pivot:
Sub CreatePivotTableSummary(wb As Workbook)
Dim WorkbookName As String
WorkbookName = wb.Name
wb.Activate
Set DataRange = Range(Selection, Selection.End(xlToRight))
Set DataRange = Range(Selection, Selection.End(xlDown))
Set OutputRange = Sheet2.Range("A3")
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataRange, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=OutputRange, TableName:=WorkbookName, DefaultVersion:=xlPivotTableVersion14
Sheet2.Select
With wb.ActiveSheet.PivotTables(WorkbookName)
.PivotFields("Portfolio").Orientation = xlRowField
.PivotFields("Portfolio").Position = 1
.PivotFields("TradePortfolio").Orientation = xlRowField
.PivotFields("TradePortfolio").Position = 2
.PivotFields("InstrType").Orientation = xlRowField
.PivotFields("InstrType").Position = 3
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("TPL"), "Sum of TPL", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("ThTPL Deco"), "Sum of ThTPL Deco", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Corr Attr"), "Sum of Corr Attr", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Cr Attr"), "Sum of Cr Attr", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Cross Effec"), "Sum of Cross Effec", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("FX Attr"), "Sum of FX Attr", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Infl Attr"), "Sum of Infl Attr", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("IR Attr"), "Sum of IR Attr", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Price Attr"), "Sum of Price Attr", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Residual"), "Sum of Residual", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Time Attr"), "Sum of Time Attr", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Trd Attr"), "Sum of Trd Attr", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("Vol Attr"), "Sum of Vol Attr", xlSum
.AddDataField ActiveSheet.PivotTables(WorkbookName).PivotFields("YC Attr"), "Sum of YC Attr", xlSum
.PivotFields("Sum of TPL").NumberFormat = "#,##0"
.PivotFields("Sum of ThTPL Deco").NumberFormat = "#,##0"
.PivotFields("Sum of Corr Attr").NumberFormat = "#,##0"
.PivotFields("Sum of Cr Attr").NumberFormat = "#,##0"
.PivotFields("Sum of Cross Effec").NumberFormat = "#,##0"
.PivotFields("Sum of FX Attr").NumberFormat = "#,##0"
.PivotFields("Sum of Infl Attr").NumberFormat = "#,##0"
.PivotFields("Sum of IR Attr").NumberFormat = "#,##0"
.PivotFields("Sum of Price Attr").NumberFormat = "#,##0"
.PivotFields("Sum of Residual").NumberFormat = "#,##0"
.PivotFields("Sum of Time Attr").NumberFormat = "#,##0"
.PivotFields("Sum of Trd Attr").NumberFormat = "#,##0"
.PivotFields("Sum of Vol Attr").NumberFormat = "#,##0"
.PivotFields("Sum of YC Attr").NumberFormat = "#,##0"
.RowAxisLayout xlTabularRow
.TableStyle2 = "PivotStyleMedium2"
.PivotFields("InstrType").PivotFilters.Add Type:=xlValueDoesNotEqual, DataField:=ActiveSheet.PivotTables(WorkbookName).PivotFields("Sum of TPL"), Value1:=0
End With
End Sub
It fails at this point:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataRange, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=OutputRange, TableName:=WorkbookName, DefaultVersion:=xlPivotTableVersion14
with the error:
Invalid procedure call or argument
Please can someone assist?
Thank you
Sometimes, we get this error when any data range does not have a column Header.
Check whether first row of your data range has some header for all columns.
Pivot table creation fails, if it could not find a column header.

Excel VBA - Stuck on generating pivot table from variable data range

I have the code below which theoretically should create a pivot table on a 2nd sheet (which exists) using the data it finds on the 'DATA' sheet. However it always crashes as soon as it reaches the part to create the pivot table.
I originally come from a fixed size and afterwards changed it so it should take all the data on my 'DATA' sheet regardless of whether it's a 2x3 or 58x13 table
Sheets("DATA").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "l"
Range("A1").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("DATA").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Prior. per user!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14
Sheets("Prior. per user").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Priority")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Caller")
.Orientation = xlRowField
.Position = 1
End With
Range("D2").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Priority").PivotItems( _
"Medium").Position = 2
Columns("D:D").ColumnWidth = 7.43
Columns("C:C").ColumnWidth = 10
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Number"), "Sum of Number", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Number")
.Caption = "Count of Number"
.Function = xlCount
End With
If anyone sees what's wrong with it, it would be much appreciated.
Your target sheet name has spaces in it so you need to enclose it in single quotes:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("DATA").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="'Prior. per user'!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14
Personally, I would also use a variable to refer to the pivot table:
Dim PT As PivotTable
Set PT = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("DATA").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable(TableDestination:="'Prior. per user'!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14)
With PT
With .PivotFields("Priority")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Caller")
.Orientation = xlRowField
.Position = 1
End With
.PivotFields("Priority").PivotItems("Medium").Position = 2
.Parent.Columns("D:D").ColumnWidth = 7.43
.Parent.Columns("C:C").ColumnWidth = 10
.AddDataField .PivotFields("Number"), "Count of Number", xlCount
End With