Create dynamic range, then pivot table VBA

gabbana

New Member
Joined
Feb 5, 2016
Messages
5
Hello all,

I'm having a bit of an issue creating a pivot table from a dynamic range. The problem I have is the data for the range is itself is dynamically generated from another filtered (huge) worksheet which changes everyday.

I can get the first sheet to open, filter, and copy all the relevant required info to a new sheet, but due to this, that sheet is, in itself new, and dynamic. (I think) I can't just use the original sheet due to file size limits for e-mails.

So, I think I somehow need to then create a range/table from the data on the new sheet, before creating the pivot. I've tried searching for an answer but can't find anything that fits.

The pivot table is reasonably simple:

Code:
Sub VariablePivot()

    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R46214C127", Version:=xlPivotTableVersion15).CreatePivotTable _
        TableDestination:="Sheet2!R3C1", TableName:="PivotTable1", DefaultVersion _
        :=xlPivotTableVersion15
    Sheets("Sheet2").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("offcat")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("offcat"), "Count of offencecat", xlCount
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("area")
        .Orientation = xlRowField
        .Position = 1
    End With
End Sub

I'm struggling to work out how to change the range to make it dynamic for the sheet it's on.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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