Amending VB variable Pivot table size

hblbs

Board Regular
Joined
Mar 18, 2009
Messages
184
Hello all

I have recorded the macro below to create a Pivot table, however I would like to use this again without having to record it. The problem I have is the the Pivot table report can be a variable length, sometime 10 rows or sometimes hundreds of row.

Below is the code, the second set of code is what I would like it to do but keep getting error messages.

Code:
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "'Completed'!R1C1:R311C8").CreatePivotTable TableDestination:="", _
        TableName:="PivotTable2", DefaultVersion:=xlPivotTableVersion10

Code:
    LastRow = ActiveSheet.UsedRange.Rows.Count
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "'Work Outstanding'!A1:H & LastRow").CreatePivotTable TableDestination:="", _
        TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10

Any help is appreciated.

Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I think this is the problem:-
Code:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "'Work Outstanding'!A1:H[COLOR=red][B]"[/B][/COLOR] & LastRow).CreatePivotTable TableDestination:="", _
        TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
If you encounter this sort of problem in the future, build the SourceData string in a VBA variable and then Debug.Print it just before you use it. Quite often the problem will become obvious.

In this case your SourceData range would have contained the string "& LastRow" instead of its value.
 
Upvote 0
Hello Ruddles

I get a run time error 1004 : The PivotTable field name is not valid. To create a PivotTable report, you must use data that is organized as a list with labeled columns. If you are changing the name of a PivotTable field, you must type a new name for the field.

Not sure why this is, but below is what I am running.

Code:
    LastRow = ActiveSheet.UsedRange.Rows.Count
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "'Work Outstanding'!A1:H" & LastRow).CreatePivotTable TableDestination:="", _
        TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Area Outstanding")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Items Outstanding"), "Sum of Items Outstanding", _
        xlSum

Thanks
 
Upvote 0
FWIW, I would probably use:
Code:
   Dim strData           As String
   Dim strSheet          As String
   
   strSheet = "Work Outstanding"
   strData = "'" & strSheet & "'!" & Sheets(strSheet).Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1)
   
   ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=strData).CreatePivotTable TableDestination:="", _
                                                   TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,896
Members
452,948
Latest member
Dupuhini

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