Creating a PivotTable with VBA returning Run-Time error 1004

phroureo

New Member
Joined
Nov 24, 2015
Messages
36
As the title says, when I try and create a PivotTable using a VBA function (I'm using the first method on this page), it returns the following error:

"Run-time error '1004':

Application-defined or object-defined error"

According to Microsoft's page about the error that it is a catch-all for anything that doesn't have a specific error, so I don't know what to do differently.

I'm going to be creating multiple pivottables in a "for" function, which is why I have it being mapped to (i, "1") and "PivotTable" & j instead of just the names.

Here is the code that I have now (I haven't even put in the "For" loop yet)

Code:
Sub Macro3()'
' Macro3 Macro
'


'


Dim pvtTbl As PivotTable
Dim wsData As Worksheet
Dim mgData As Range
Dim pvtTblCache As PivotCache
Dim wsPvtTbl As Worksheet
Dim pvtFld As PivotField
Dim i As Integer
Dim j As Integer


i = 1
j = 1


'What worksheet contains the data
Set wsData = Worksheets("Data Import")


'where the new pivot table will be created
Set wsPvtTbl = Worksheets("Profit and Loss Statement")


'create the pivot cache
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:="Table_name", _
        Version:=xlPivotTableVersion12).CreatePivotTable _
 _
        TableDestination:=wsPvtTbl.Cells(i, "1"), _
        TableName:="PivotTable" & j, _
        DefaultVersion:=xlPivotTableVersion12
    End Sub

The error is (obviously) happening on the "create pivot cache" section. Unfortunately, you can't break up the pivot table creation by row, so I can't get more specific. Also, I've tried using a macro to create a PivotTable and then immediately deleting it and rerunning the macro, but that returns the same 1004 error.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try replacing...

Code:
TableDestination:=wsPvtTbl.Cells(i, "1")

with

Code:
TableDestination:=wsPvtTbl.Cells(i, [COLOR=#ff0000]1[/COLOR])

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top