Need help in creating a Pivot table in excel sheet using VBA

bhandarx

New Member
Joined
Jul 8, 2014
Messages
15
Hi,

I created a pivot table using the vba code shown below. I needed to achieve few other changes with respect to this and need your help on the same. I am using Excel 2010.

1. The code is creating the pivot table in new sheets (like Sheet1, Sheet2, Sheet3 etc) on re-executing the code every time. I need to know how to ensure that the pivot table is created in a new sheet with a name of my choice? i.e. Whenever I run the macro the pivot table should be created in a new sheet named as "MyChoice"

2. If the macro is being run for the second time, then the code should be able to delete the existing pivot table in the sheet say "MyChoice" and create the new Pivot Table in the same sheet "MyChoice"

<Code>

Dim objTable As PivotTable, objField As PivotField

' Select the sheet and first cell of the table that contains the data.
ActiveWorkbook.Sheets("RAWDATA").Select
Range("A1").Select

' Create the PivotTable object based on the Employee data on Sheet1.
Set objTable = ActiveWorkbook.Sheets("RAWDATA").PivotTableWizard
objTable.Name = "Consumption Pivot"

' Specify row and column fields.
Set objField = objTable.PivotFields("PART DESCRIPTION")
objField.Orientation = xlRowField

Set objField = objTable.PivotFields("DESCRIPTION")
objField.Orientation = xlRowField

'objField.Orientation = xlColumnField

' Specify a data field with its summary
' function and format.
Set objField = objTable.PivotFields("QUANTITY")
objField.Orientation = xlDataField
objField.Function = xlSum


' Specify a page field.
Set objField = objTable.PivotFields("FAMILY")
objField.Orientation = xlPageField

Set objField = objTable.PivotFields("BUSINESS AREA")
objField.Orientation = xlPageField

</code>


Could you please help me to achieve this?


Best Regards,
bhandarx
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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