variable data range for pivot table in macro

jess89

New Member
Joined
Jul 26, 2011
Messages
3
Hi

I have a macro, that contains the creation of a pivot table. Unfortunately, the data for the pivot table is not always the same size. How can I adjust my macro, that the data for the pivot may be variable?

Here is the part of my macro, that creates the pivot table:

Sheets("Data").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R521C36", Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:="pivottemp!R2C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10
Sheets("pivottemp").Select

-> How can I adjust the "SourceData:=_" part?


Thanks for your help
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try somthing like this

Code:
Set PTCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:="'" & ActiveSheet.Name & "'!" & Range("A3").CurrentRegion.Address(True, True, xlR1C1))
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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