+ Reply to Thread
Results 1 to 7 of 7

Pivot Table Macro Needed

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Pivot Table Macro Needed

    Hi

    I have a couple of pieces of code from other macros and I want to combine them to make one piece of code if possible.

    I want to create a pivot table in a new worksheet using the data in the Sheet Report1.

    The data starts in A2 and goes across to Q2 and the end of the data varies.

    I have this which will count the rows in column A.

        Lr1 = Range("A" & Rows.Count).End(xlUp).Row
    I have then amended that to take off the header and sum columns.

        Lr1 = Lr1 - 2
    Then I have this to select the range.

        With Range("A2:Q" & Lr1)

    I want to put this in here but don't know how to.

        ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
            "Report1!R1C1:R6826C18").CreatePivotTable TableDestination:="", TableName:= _
            "PivotTable2", DefaultVersion:=xlPivotTableVersion10
        ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
        ActiveSheet.Cells(3, 1).Select
        ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array("Imprint", _
            "DAC Market", "Data")
        With ActiveSheet.PivotTables("PivotTable2").PivotFields("Net Val")
            .Orientation = xlDataField
            .Position = 1
        End With
        With ActiveSheet.PivotTables("PivotTable2").PivotFields("Margin")
            .Orientation = xlDataField
            .Position = 2
        End With
        With ActiveSheet.PivotTables("PivotTable2").PivotFields("Home Return")
            .Orientation = xlDataField
            .Caption = "Sum of Home Return"
            .Function = xlSum
        End With
        ActiveCell.Offset(2, 2).Range("A1").Select
        With ActiveSheet.PivotTables("PivotTable2").DataPivotField
            .Orientation = xlColumnField
            .Position = 1
    
        End With

    Basically getting rid of the hard coding of the range in the pivot table part.

    Any help much appreciated.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: Pivot Table Macro Needed

    Do you mean your headers are in row 2 on the data sheet? Why are you subtracting 2 and not 1 from the last row variable?
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Pivot Table Macro Needed

    My headers are in row 1 and the total for the sheet is in the very last row which is variable. So I thought I needed to take off 2.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: Pivot Table Macro Needed

    Nope - you need to include headers for the pivot table to use as field names. So:

        Dim LR1                   As Long
        Dim PT                    As PivotTable
    
        LR1 = Range("A" & Rows.Count).End(xlUp).Row - 1
    
        Set PT = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
                    "Report1!R1C1:R" & LR1 & "C17").CreatePivotTable(TableDestination:=Sheets.Add.Cells(1, 3), _
                    DefaultVersion:=xlPivotTableVersion10)
    
        With PT
            .AddFields RowFields:=Array("Imprint", "DAC Market", "Data")
            With .PivotFields("Net Val")
                .Orientation = xlDataField
                .Position = 1
            End With
            With .PivotFields("Margin")
                .Orientation = xlDataField
                .Position = 2
            End With
            With .PivotFields("Home Return")
                .Orientation = xlDataField
                .Caption = "Sum of Home Return"
                .Function = xlSum
            End With
            With .DataPivotField
                .Orientation = xlColumnField
                .Position = 1
            End With
        End With
    Note I changed the C18 to C17 since you said column Q, not R.

  5. #5
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Pivot Table Macro Needed

    Hi romperstomper,

    This works almost perfectly. How can i make the pivot table start in A1 of the new sheet? It is spread across C:G.

    Regards

    Tim

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: Pivot Table Macro Needed

    You need to change this bit:
    Sheets.Add.Cells(1, 3)
    You can switch it to:
    Sheets.Add.Range("A1")

  7. #7
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Pivot Table Macro Needed

    Brilliant, many thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. complex macro needed for pivot table
    By Exceler54 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-08-2014, 01:18 PM
  2. Macro Help Needed - Link Pivot Table Filter to Regular Table AutoFilter
    By ycx1129 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-11-2011, 12:54 PM
  3. Macro needed for pivot table
    By PlanetGong in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2009, 05:55 PM
  4. Help needed with Pivot table macro to remove data
    By Nelly in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2006, 09:00 PM
  5. [SOLVED] Macro Needed for Pivot Table
    By douglascfast@hotmail.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-12-2005, 12:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1