Macro - Create Pivot Table

Mundy727

New Member
Joined
Jul 7, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

New to the forums here. I've been working on a macro to create multiple pivot tables in new, different worksheets. I have this broken down into 2 steps or 2 separate Macros (Step 1 creates 1 pivot table in a new sheet and step 2 creates a separate pivot table in another new sheet). I cant seem to figure out why Step 1 works fine and creates the pivot table to add to a new sheet, and then step 2 does not work. I've provided VBAs for the below.

Also, I'm running Step 1 macro and then running Step 2, not sure if this quite makes a difference.

Appreciate your assistance! Thank you!

Step 1

Rich (BB code):
Dim dataname As String
Dim newsheet As String

dataname = ActiveSheet.ListObjects(1).Name

    Application.CutCopyMode = False
    Sheets.Add
    newsheet = ActiveSheet.Name
   
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        dataname, Version:=7).CreatePivotTable TableDestination:=newsheet & "!R3C1", _
        TableName:="PivotTable2", DefaultVersion:=7
    Sheets(newsheet).Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable2")

Step_2 (Doesnt work - syntax error somewhere in red below)

Rich (BB code):
Dim dataname As String
Dim newsheet As String

dataname = ActiveSheet.ListObjects(1).Name

    Application.CutCopyMode = False
    Sheets.Add
    newsheet = ActiveSheet.Name
   
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        dataname, Version:=7).CreatePivotTable TableDestination:=newsheet & "!R3C1",
        TableName:="PivotTable4", DefaultVersion:=7
    Sheets(newsheet).Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable4")
 
Last edited by a moderator:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi, it seems to lack "_" on the second line in red for making VBA understand it's one code line.

VBA Code:
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
                                      dataname, Version:=7).CreatePivotTable TableDestination:=newsheet & "!R3C1", _
    TableName:="PivotTable4", DefaultVersion:=7
 
Upvote 0
Solution
Thank you Colo! I updated via the below screenshot and still shows Syntax error. Any thoughts?

Appreciate your help!
 

Attachments

  • Capture.PNG
    Capture.PNG
    31.9 KB · Views: 26
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

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