I am finding this issue \"Run-time error \'5\' Invalid procedure call or argument\" when defining the PivotCache on my code. The Excel I am using is 2016 MSO (16.0.4266.1001) 32
When creating a PivotCache
, MSDN recommends using a String
reference to specify the workbook, worksheet and cell range for SourceData
, instead of passing a Range
object.
Instead of using PRange
(which you've forgotten to Set
here), you can use R1C1 notation, like this answer suggests.
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
... SourceData:="'US Master Macro'!R1C1:R" & LastRow & "C" & LastCol
Just in case anyone is encountering a similar problem and the above answer does not solve, if you are using Slicers which are connected to various pivots (all of which come from the same data source) then the ChangePivotCache will not work unless you disconnect the pivots from the slicer first. A few days of searching through questions/answers and finally came across the problem!