Creating pivot tables VBA

mfizz

Board Regular
Joined
Dec 18, 2013
Messages
96
I am using a range to create pivot tables. Unfortunately the length of the sheet changes hence the range and therefore "sourcedata" changes.

Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R2741C63", Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Sheet4!R3C1", TableName:="PivotTable3", DefaultVersion _
        :=xlPivotTableVersion14
    Sheets("Sheet4").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("COUNT = 6")
        .Orientation = xlRowField
        .Position = 1
    End With

how can i tell excel to use the new range everytime using the r1c1 style.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You can use CurrentRegion:
Code:
With Sheets("Sheet1")
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "'" & .Name & "'!" & .range("A1").Currentregion.address(referencestyle:=xlr1c1), Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Sheet4!R3C1", TableName:="PivotTable3", DefaultVersion _
        :=xlPivotTableVersion14
End With
 
Upvote 0
Code:
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _        "'" & .Name & "'!" & .Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1), Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Sheet4!R3C1", TableName:="PivotTable3", DefaultVersion _
        :=xlPivotTableVersion14

Using this i get an unqualified reference error.
 
Upvote 0
That will happen if you simply remove bits of the code I posted! ;)
You need the With ... and End With lines too.
 
Upvote 0
hahah sweet.. sorted mate thanks! you can understand i have been coding all day started lose pieces of my brain
 
Upvote 0
I know exactly how you feel! :)
 
Upvote 0
I've posted a suggestion.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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