Macro Error for Pivot Table

Copper Contributor

I can create a Pivot Table for the data I download and it works fine.

However, when I made a Macro to do the same thing, I get an error. The error is: Run-time error '5':

 

The following is a screenshot of the highlighted error in the Macro. I am now using Excel 2013, however I made an initial Macro using an older version of Excel and am now trying to make a pivot table using 2013 and installing it near the start of the existing Macro I have.

Macro Error for Pivot TableMacro Error for Pivot Table

How do I correct for this error?

8 Replies

@WayneEK Does "Table1" exist? And what about the destination "Contract1 Report"?

 

If either one of those doesn't exist, or is even spelled differently, then that would cause the code to bug.

Thanks for replying. I confirmed the spelling and reference to the sheet and table is correct but am still getting the error. The only thing I can think it might be is that I should create a separate Sheet to put the Pivot Table on, instead of the existing sheet that the table is on. Thanks anyways.

@WayneEK try recording a new macro to create the pivot table then compare both codes

@WayneEK did you ever resolve this?  I am getting a very similar message and it is indicating the bug is within the pivot portion.  I built the macro using the record macro feature and not the line by line code.  Any advice?

@Shannon8000 

I ran into this same issue. In Excel 2013, the PivotTable version number is different than if you are using other versions.

 

When using Excel 2013, you must change Version:=6 to Version:=xlPivotTableVersion15. Change DefaultVersion:=6 to DefaultVersion:=xlPivotTableVersion15.

 

Excel 2013 VBA script for creating PivotTable:

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R4000C6", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination:= _
"Template!R3C1", TableName:="SalesTaxTable", DefaultVersion:=xlPivotTableVersion15

@bsand210801 

 

I am running into same error in Microsoft 365. Was there ever a fix?

@RichardSeeley 

I am currently trying to run a macro that I made which contains that step, even after converting the macro over into the personal folder, I am unable to run that macro on my co-worker's computers. Is there some tip anyone can give me to share macros seemlessly?

@RichardSeeley 

 

I have a similar issue noting that I'm trying create 1 pivot table (template) with the same edits in font size, filter, and column width edits, and column title change),  so the macro will duplicate the table 6 times into 6 new sheets.  Then I'd like to rearrange the categories in the Table Row within PivotTable Fields to look differently at the table data in each sheet..

 

VBA runs, creates the 1st pivot, but on the copy/paste to a new sheet step,  it seems to drop the very 1st row category inside the PivotTable Fields section (noting it was there when macro was created and is within the VBA script-hence the error)

ninjaryder_2-1709598321711.png

 

"code scan" is not even generated under the first Table created from the macro. 

ninjaryder_1-1709598203065.png

***Even the 1st PV created (3rd image) doesn't contain the 1st row category (Code Scanned Y/N) noted in the macro VBA code for position 1 (1st image)

 


ninjaryder_0-1709598151849.png

*** the missing category propagates upon creating the next copied table noting it could not find position 2 category name.

 

 

Does something happen when you rearrange the categorical values in the Rows box of PivotTable Fields?   Even though they are 6 independent tables since they were copied/pasted?