Dynamic Range Pivot Table

clueless1

New Member
Joined
Apr 16, 2013
Messages
17
Hi There,

I've looked at many posts on this subject and unfortunately I cant seem to use them to stitch together a solution, so hope this new thread doesn't annoy people.

I have a workbook with just one worksheet in it, called 'Template_data'. The number of rows in this could change, so I want to select the range dynamically and then add a Pivot Table to a new worksheet called 'Summary by Provider'. The pivot table VBA I can get from recording the marco, but its selecting the region and then telling it to use that region for the pivot table and to then insert the pivot table onto a new sheet called 'Summary by Provider'. I've tried many different methods and now feel lost, can anyone help please?

Code:
Sub DynamicRange1()'Best used when your data does not have any entirely blank rows or columns


Dim sht As Worksheet
Dim StartCell As Range


Set sht = Worksheets("Template_data")
Set StartCell = Range("A1")


'Select Range
StartCell.CurrentRegion.Select


With ActiveSheet.PivotTables("PivotTable12").PivotFields("ServiceProvider")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable12").AddDataField ActiveSheet.PivotTables( _
        "PivotTable12").PivotFields("PlanValue_WrapSipp"), _
        "Count of PlanValue_WrapSipp", xlCount
    With ActiveSheet.PivotTables("PivotTable12").PivotFields( _
        "Plan Valuation Type Group")
        .Orientation = xlPageField
        .Position = 1
    End With
  


End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Instead of this:

Code:
StartCell.CurrentRegion.Select

Try This:
Code:
sht.UsedRange.Select
 
Last edited:
Upvote 0
ok, thank you, now I need to insert a new worksheet, name it and add the pivot table to it - can you help please? I'm thinking I may need to store the sht.UsedRange.Select into a Range variable to use for this part of Pivot table: "SourceData:=" ?

Code:
Sub DynamicRange1()'Best used when your data does not have any entirely blank rows or columns


Dim sht As Worksheet
Dim StartCell As Range


Set sht = Worksheets("Template_data")


sht.UsedRange.Select




With ActiveSheet.PivotTables("PivotTable12").PivotFields("ServiceProvider")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable12").AddDataField ActiveSheet.PivotTables( _
        "PivotTable12").PivotFields("PlanValue_WrapSipp"), _
        "Count of PlanValue_WrapSipp", xlCount
    With ActiveSheet.PivotTables("PivotTable12").PivotFields( _
        "Plan Valuation Type Group")
        .Orientation = xlPageField
        .Position = 1
    End With
  


End Sub
 
Upvote 0
Try this.

Code:
Sub DynamicRange1() 'Best used when your data does not have any entirely blank rows or columns

Dim sht As Worksheet
Dim StartCell As Range

Set sht = Worksheets("Template_data")
sht.Select
sht.UsedRange.Select
Findstring = Selection.Address
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Select
ActiveSheet.Name = "Pivot"
Set sht1 = Worksheets("Pivot")
sht.Select
Dim Pt As PivotTable
Set Pt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    Findstring, _
    Version:=xlPivotTableVersion14).CreatePivotTable(TableDestination:="Pivot!R1C1", TableName:="PivotTable1", _
    DefaultVersion:=xlPivotTableVersion14)
sht1.Select
End Sub
 
Upvote 0
Hi Pranuvins,

thank you but I receive an error of 'Run-time error '5': Invalid procedure call or argument'

And the line of code concerned it highlights is:

Code:
[/COLOR]Set Pt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SData, Version:=xlPivotTableVersion14).CreatePivotTable(Tabledestination:="Summary by Provider!R3C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14)
[COLOR=#574123]

+ When I hover over Pt it states 'Pt = Nothing'

Code:
[/COLOR]Sub DynamicRange1()


Dim sht As Worksheet
Dim StartCell As Range
'Dim Pt As PivotTable




Set sht = Worksheets("Template_data")


sht.Select
sht.UsedRange.Select
SData = Selection.Address
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Select
ActiveSheet.Name = "Summary by Provider"


Set sht1 = Worksheets("Summary by Provider")
sht1.Select


Dim Pt As PivotTable
Set Pt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SData, Version:=xlPivotTableVersion14).CreatePivotTable(Tabledestination:="Summary by Provider!R3C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14)
sht1.Select


With ActiveSheet.PivotTables("PivotTable1").PivotFields("ServiceProvider")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("PlanValue_WrapSipp"), _
        "Count of PlanValue_WrapSipp", xlCount
    With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
        "Plan Valuation Type Group")
        .Orientation = xlPageField
        .Position = 1
    End With
  


End Sub
[COLOR=#574123]

Thanks for your help
Pranuvins
 
Upvote 0
Hi,

Replace sht1.Select to sht.Select

Code:
Set sht1 = Worksheets("Summary by Provider")
sht.Select
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,123
Members
452,546
Latest member
Rafafa

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