Create pivot table in the same worksheet using excel VBA

BMD44

Board Regular
Joined
Sep 25, 2019
Messages
72
Hello,

I have below code which creates pivot table sheet after the current sheet. Can anyone please suggest how to create pivot table in the same sheet.

Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("SrcData")

Thanks in advance.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi there

Have you maybe tried the following?

VBA Code:
Sub Macro2()
Dim MyRange As Range
Set MyRange = ActiveSheet.Range("G1") 'Your range on the sheet where Pivot Table needs to go. Also remeber to update SourceData Table1 to your table name
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
             "Table1", Version:=8).CreatePivotTable TableDestination:=MyRange, _
               TableName:="PivotTable3", DefaultVersion:=8
End Sub
 
Upvote 2
Solution
Thank you.

I have tried below and it gives error. Can you please let me know where can I specify source range details here

VBA Code:
Sub Button4_Click()
Dim MyRange As Range
Set MyRange = ActiveSheet.Range("B25") 'Your range on the sheet where Pivot Table needs to go. Also remeber to update SourceData Table1 to your table name
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
             "JE Detail", Version:=8).CreatePivotTable TableDestination:=MyRange, _
               TableName:="PivotTable3", DefaultVersion:=8
End Sub
 
Upvote 0
I have renamed Jimmy's variable to make it a little more obvious:
Note: Jimmy used a Table as his data source and you would be well advised to do the same. That way it will automatically expand if you add more data.

Rich (BB code):
Sub Button4_Click()
Dim rngDestPvt As Range
Set rngDestPvt = ActiveSheet.Range("B25")   'Your range on the sheet where Pivot Table needs to go. Also remeber to update SourceData Table1 to your table name
   
Dim rngSrcData As Range
Set rngSrcData = Range("A1").CurrentRegion  'Your range where the source data is

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSrcData, _
                Version:=8).CreatePivotTable TableDestination:=rngDestPvt , _
                TableName:="PivotTable3", DefaultVersion:=8
End Sub
 
Last edited:
Upvote 1
I have renamed Jimmy's variable to make it a little more obvious:
Note: Jimmy used a Table as his data source and you would be well advised to do the same. That way it will automatically expand if you add more data.

Rich (BB code):
Sub Button4_Click()
Dim rngDestPvt As Range
Set rngDestPvt = ActiveSheet.Range("B25")   'Your range on the sheet where Pivot Table needs to go. Also remeber to update SourceData Table1 to your table name
  
Dim rngSrcData As Range
Set rngSrcData = Range("A1").CurrentRegion  'Your range where the source data is

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSrcData, _
                Version:=8).CreatePivotTable TableDestination:=rngDestPvt , _
                TableName:="PivotTable3", DefaultVersion:=8
End Sub
Thank you. This works.
 
Upvote 1
Thanks for letting us know. Glad we could help.
PS: I think @Jimmypop did the hard part ;)
Thanks Alex.. Lol was actually quite simple... I just used the macro recorder and then just updated the code generated to declare and set the range... Looking at it now I could have gone deeper and declared / set the source and destination ranges as you did but it was done during a quick lunch break😅
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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