Problems specifying ranges to generate pivot tables

sdorairaj

New Member
Joined
Apr 2, 2002
Messages
2
This is probably a very basic question I know, but here goes anyway.

I am trying to write a macro that organises data extracted from Access into Excel Pivot tables.

I have pivot table source data sitting in a worksheet and want it to be separated into several pivot tables based on the value in column D.(i.e. one pivot Table for each different value in column D).

My problem is that the number of rows associated with each value in Column D is always changing thus I am trying to write a macro that puts all the data assocaited with one column D value into a pivot table.

this is the code I have written to do this.


Sub Parameterptable()
Dim Ptable As Range
Dim PTCache As PivotCache
Dim PT As PivotTable

Set WorkRange = Worksheets("Sheet1").Range("D3").CurrentRegion
'Use TopCell to set upper limit of range
'Use BottomCell to set lower limit of range
Set TopCell = Range("D3")
Set BottomCell = TopCell.End(xlDown)
Do While BottomCell <> TopCell
Set BottomCell = (BottomCell.Offset(-1, 0))
Loop

'Select range TopCell to BottomCell as source data for Pivot table 1
Set Ptable = Range(TopCell, BottomCell).EntireRow.Select

Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Range(Ptable))
Set PT = PTCache.CreatPivotTable_(TableDestination:=" ", TableName:="PivotTable1")
With PT
.PivotFields("C3").Orientation = xlColumnField
.PivotFields("C3").Orientation = xlRowField
.PivotFields("G3").Orientation = xlDataField
End With

End Sub


NOw
a. I know this may not be the most efficient way to do this - any advice greatly appreciated
b. when I run this script i get one of 2 error messages either "object not defined", or Range("ptable") =< Method "range of object ' _ Global' failed

Can anyone tell me what the problem might be,

Thanks,

Sumi
 

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,213,497
Messages
6,113,999
Members
448,541
Latest member
iparraguirre89

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