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