Dynamic range pivot tables in VBA

EcoWise

New Member
Joined
Jul 7, 2012
Messages
1
Hi

I'm a new VBA user and need to create a Pivot Table that uses data from a dynamic range. Also, the destination of the pivot table I want to be in a separate worksheet form the actual database (within the same workbook).

Having done some research, I have used the following code;

Option Explicit


Sub Pivot_with_Dynamic_range()


Sheets("ExpenditureBasicsPVT").Activate
Dim ExpenditureBasicsPVT As Worksheet
Sheets("DataEntry").Activate
Dim DataEntry As Worksheet
Dim PTCache As PivotCache
Dim pt As PivotTable
Dim PRange As Range
Dim FinalRow As Long
Dim FinalCol As Long
Set ExpenditureBasicsPVT = Worksheets("PivotTable")


For Each pt In ExpenditureBasicsPVT.PivotTables
pt.TableRange2.Clear
Next pt
Sheets("DataEntry").Activate
Cells(1, 3).Select
FinalRow = DataEntry.Cells(Rows.Count, 3).End(xlUp).Row
FinalCol = DataEntry.Cells(Columns.Count, 1).End(xlToLeft).Column
Set PRange = DataEntry.Cells(1, 1).Resize(FinalRow, FinalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)
Set pt = PTCache.CreatePivotTable(TableDestination:=ExpenditureBasicsPVT.Cells(3, 2), TableName:="PivotTable1")



End Sub

The Data is located in the "DataEntry" worksheet and begins at cell "C1."
The pivot table destination must be in the worksheet, "ExpenditureBasicsPVT".

The above code gave me an "Run-time error '9', Subscript out of range" error when I tried to run it.

Could any one shed some light on this error.

Many thanks!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Best option is to name a dynamic range and use this as the DataSource for the PT.
In order to assign dinamic range to a name (say your source information has new rows from time to time), simply do this:
- go to name manager and create NEW;
- define it as per this standard (adjust the names and columns to your file): "=SheetName!$A$1:OFFSET(SheetName!$A$1,COUNTA(SheetName!$A:$A)-1,0)";
- use the given name as the datasource in your PT wizard;
- voilá! your PT datasource will Always take the correct range.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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