VBA and Pivot Tables

rmcginnis77

New Member
Joined
Apr 4, 2011
Messages
1
I'm trying to create a Pivot Table via VBA. The tool I'm using gives me a skeleton and I created the pivot code via the macro recorder.

What I can't figur out is what other objects I need to define (and how)
The first code is the skeleton, the second part is the excel macro.


Sub QC_PostProcessing()
Dim MainWorksheet As Worksheet
Make sure your worksheet name matches!
Set MainWorksheet = ActiveWorkbook.Worksheets("Query1")
Dim DataRange As Range
Set DataRange = MainWorksheet.UsedRange

' Now that you have the data in DataRange you can process it.

Range("G1").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Range").CreatePivotTable TableDestination:= _
"Query1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10
Charts.Add
ActiveChart.Location Where:=xlLocationAsNewSheet

.
.
.
Running the macro fails just after the range designation.

Thank you
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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