+ Reply to Thread
Results 1 to 1 of 1

Trouble with pivot table creation macro

Hybrid View

  1. #1
    Registered User
    Join Date
    08-09-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Question Trouble with pivot table creation macro

    Hi All. I tried creating a macro that generates a pivot table and sticks it in a new tab when a button is pressed. It sort of worked but had issues. So here is what I am trying to accomplish:

    1. Push a button on a "GUI" tab to create the pivot table (ie, the button is on another tab than the data and eventual pivot table)
    2. Old pivot table is deleted and new is created in the "Pivot" tab based on the data in the "Data" tab.
    3. Row fields are "Preferred Supplier", "M-spec", and "PO or Plan".
    4. Column Field is "Due Date".
    5. Data field is "OpenQty".
    6. Date must be grouped by month

    Here is the code I have that is creating the table in a new (random) tab. Problem is, I can't figure out how to specify which tab to create the table on, and I can't figure out how to group by month.

    Sub Pivot2()
        Dim WSD As Worksheet
        Dim PTCache As PivotCache
        Dim PT As PivotTable
        Dim PRange As Range
        Dim FinalRow As Long
    
        Set WSD = Worksheets("PO")
        Dim WSR As Worksheet
    
        ' Delete any prior pivot tables
        For Each PT In WSD.PivotTables
            PT.TableRange2.Clear
        Next PT
    
        ' Define input area and set up a Pivot Cache
        FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
        FinalCol = WSD.Cells(1, Application.Columns.Count). _
            End(xlToLeft).Column
        Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
        Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:= _
            xlDatabase, SourceData:=PRange.Address)
    
        ' Create the Pivot Table from the Pivot Cache
        Set PT = PTCache.CreatePivotTable("", TableName:="PivotTable1")
    
        ' Turn off updating while building the table
        PT.ManualUpdate = True
    
        ' Set up the row fields
        PT.AddFields RowFields:=Array("M-Spec", "Prefered Supplier", "PO or Plan"), ColumnFields:="DueDate"
    
        ' Set up the data fields
        With PT.PivotFields("OpenQty")
            .Orientation = xlDataField
            .Function = xlSum
            .Position = 1
            '.NumberFormat = "#,##0"
        End With
    
        ' Ensure that you get zeroes instead of blanks in the data area
        PT.NullString = "0"
    
        ' Calc the pivot table to allow the date label to be drawn
        PT.ManualUpdate = False
        PT.ManualUpdate = True
    
        ' Calc the pivot table
        PT.ManualUpdate = False
        PT.ManualUpdate = True
    
    End Sub
    Last edited by clawlan; 08-09-2010 at 11:57 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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