Macro I recorded won't run

MarvinH

New Member
Joined
Jul 29, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi I'm a complete beginner with macros, I have 8 excel report I do every week that use the same format and tabs, I want to automate this to save me some time, I have recorded a macro in spreadsheet but I keep getting run time error 1004, can you help me understand where I am going wrong please?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Paste the code in here so we can see.

When it errors click Debug, it'll highlight a line of code, show us which line too. :)
 
Upvote 0
Hi thanks for the reply i get this error on my screen in yellow

Sub Macro1()
'
' Macro1 Macro
'

'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=6).CreatePivotTable TableDestination:="Sheet3!R3C1", _
TableName:="PivotTable2", DefaultVersion:=6
 
Upvote 0
Paste the code in here so we can see.

When it errors click Debug, it'll highlight a line of code, show us which line too. :)

Hi thanks for the reply i get this error on my screen in yellow

Sub Macro1()
'
' Macro1 Macro
'

'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=6).CreatePivotTable TableDestination:="Sheet3!R3C1", _
TableName:="PivotTable2", DefaultVersion:=6
 
Upvote 0
Thanks, You're adding a sheet that is generating a name then trying to add a Pivot to Sheet3. Is this what you want? Does Sheet3 exist?
 
Last edited:
Upvote 0
Maybe try this

VBA Code:
Sub AddPivot()

Dim ws As Worksheet

Sheets.Add.Name = "NewSheet"
Set ws = Sheets("NewSheet")

    Application.CutCopyMode = False
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Table1" , Version:=6).CreatePivotTable TableDestination:= _
        ws.Range("A3"), TableName:="PivotTable2", DefaultVersion:=6

End Sub
 
Upvote 0
Using code to create a pivot table is not something that I've tried doing so I may be misreading the code, but it looks like you're trying to create an object that already exists.
 
Upvote 0
Hi all, yeah sheet 3 does not exist but I have run into this problem following your code

Sub Macro2()

'

' Macro2 Macro

'



'





Dim ws As Worksheet



Sheets.Add.Name = "Newsheet"

Set ws = Sheets("Newsheet")



Application.CutCopyMode = False

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _

"Table1", Version:=6).CreatePivotTable TableDestination:= _ ws.Range("A3"),

TableName:="PivotTable3", DefaultVersion:=6

Sheets("Sheet4").Select
 
Upvote 0
Code I pasted runs fine for me. Is it 1004 again on Sheets("Sheet4").Select? Does it exist?
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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