Share via

Pivot table as dynamic range in VBA

Anonymous
2015-07-02T11:56:30+00:00

How can a pivot table be programmed as the printing area?

Now I have a macro that generates a pivot table (if there is better code to do this please advice)

On the same sheet as the pivot table there is also other data but only the pivot table needs to be printed.

There seems to be a possibility using dynamic ranges but no idea how to use that to get where I want.

Other methods are also welcome as long as it can be done by the macro.

 ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Range("A1:E" & Range("A" & Columns("A").End(xlDown).Row).Row), Version:=xlPivotTableVersion10).CreatePivotTable _

        TableDestination:=Selection, TableName:="Draaitabel6", _

        DefaultVersion:=xlPivotTableVersion10

    With ActiveSheet.PivotTables("Draaitabel6").PivotFields("Soort kader") 'Range("E1"))

        .Orientation = xlRowField

        .Position = 1

    End With

    With ActiveSheet.PivotTables("Draaitabel6").PivotFields("Afmetingen")

        .Orientation = xlRowField

        .Position = 2

    End With

    ActiveSheet.PivotTables("Draaitabel6").AddDataField ActiveSheet.PivotTables( _

        "Draaitabel6").PivotFields("Aantal"), strJobAndProject, xlSum

    ActiveSheet.PivotTables("Draaitabel6").TableStyle2 = "PivotStyleMedium9"

Thanks for any help

Frank

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-07-02T14:51:31+00:00

    Sorry, not an option.

    The pivot table is genereted using an other macro file.

    The file containing the pivot table should have no code, only formulas and data, so the print area should be controled by some form of formula (dynamic or something) so that the print area changes when the size of the pivot table changes but whithout using code.

    Can this be done?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-07-02T14:22:28+00:00

    I am not sure if this will work, try it

    ActiveSheet.PivotTables("Draaitabel6").PivotCache.Refresh

    ActiveSheet.PivotTables("Draaitabel6").PivotSelect "", xlDataAndLabel, True

    ActiveSheet.PageSetup.PrintArea = Selection.Address

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-07-02T14:00:07+00:00

     ActiveSheet.PivotTables("Draaitabel6").PivotSelect "", xlDataAndLabel, True

     ActiveSheet.PageSetup.PrintArea = Selection.Address         

    This code does set the print area to the pivot table but when the data changes in the table, the print area does'nt change and refers still to the first pivot table size

    Is there a way to get the print area to adapt to the changes of the pivot table?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2015-07-02T13:35:20+00:00

    ActiveSheet.PivotTables("Draaitabel6").PivotSelect "", xlDataAndLabel, True

     ActiveSheet.PageSetup.PrintArea = Selection.Address

    Was this answer helpful?

    0 comments No comments