Macro - Insert Pivot Table in Wkst - Excel 2010

NEW_2VBA

Board Regular
Joined
Dec 15, 2015
Messages
106
Warm Greetings & Happy Tuesday! :cool:

I used Macro Recorder to create module that will insert a pivot table on a new worksheet titled "Invoice Pivot". The source worksheet will always be the same but the number of rows will change.
Issues I'm having with this code are:
A) How to specify worksheet should be added titled "Invoice Pivot" towards beginning
B) Range should reflect count # of rows for each wkst in future
C) Pivot cache - should this to be addressed in code? Read about it but not 100% clear
D) Future updates - should I worry about preventing table from updating each time source data sheet changes? If so, what verbiage can be included to manually update pivot table in future? Read in Excel Macros for Dummies :eek:) about suspending automatic pivot table recalculations to prevent slowness.

Sorry if this sounds scattered; it's simply a reflection of my brain's condition in trying to understand Macros/VBA code! Totally new to this. Thanks in advance!



Sub CreatePVT()
'
' CreatePVT Macro
'

'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Invoice!R1C1:R388C18", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet2!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet2").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Invoice#")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("InvoiceDate")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("RequestAmount"), "Sum of RequestAmount", xlSum
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Invoice Pivot"
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,215,480
Messages
6,125,049
Members
449,206
Latest member
Healthydogs

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