Results 1 to 20 of 20

Thread: RE: Pivot table creation in existing sheet- VBA issues

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    Here's something to consider -- my guess as to where you wanted to get to

    Some of that code was way more complicated than needed


    Option Explicit
    
    
    Sub createPivotTableExistingSheet()
        Dim rMainData As Range
        Dim wsPT As Worksheet, wsItem As Worksheet
        Dim myPivotTable As PivotTable
        Dim ptItem As PivotItem
     
        Set rMainData = MainData.Cells(1, 1).CurrentRegion      '   Note - Using code name for WS "Main Data"
     
        Set wsPT = AddSheet("PT")
     
         Set myPivotTable = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
            SourceData:=rMainData).CreatePivotTable(TableDestination:=wsPT.Cells(1, 1), TableName:="PivotTableExistingSheet")
     
        With myPivotTable
            .PivotFields("Item").Orientation = xlPageField
            .PivotFields("OrderDate").Orientation = xlRowField
            .PivotFields("Region").Orientation = xlRowField
            .PivotFields("Rep").Orientation = xlRowField
            .PivotFields("Units").Orientation = xlRowField
            .PivotFields("Unit Cost").Orientation = xlRowField
            With .PivotFields("Total")
                .Orientation = xlDataField
                .Position = 1
                .Function = xlSum
                .NumberFormat = "#,##0.00"
            End With
        
            .RowAxisLayout xlTabularRow
            
            .PivotFields("OrderDate").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
            .PivotFields("Region").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
            .PivotFields("Rep").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
            .PivotFields("Item").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
            .PivotFields("Units").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
            .PivotFields("Unit Cost").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
            .PivotFields("Total").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
            
            .ColumnGrand = False
            .RowGrand = False
            
            .PivotFields("OrderDate").AutoSort xlAscending, "OrderDate"
            .PivotFields("Region").AutoSort xlAscending, "Region"
            .PivotFields("Rep").AutoSort xlAscending, "Rep"
            
            For Each ptItem In .PivotFields("Item").PivotItems
                .PivotFields("Item").ClearAllFilters
                .PivotFields("Item").CurrentPage = ptItem.Value
            
                Set wsItem = AddSheet(ptItem.Value)
                        
                .TableRange1.Copy
                wsItem.Cells(1, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                wsItem.Cells(1, 1).CurrentRegion.Font.Bold = False
                wsItem.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
            Next
        End With
    End Sub
    
    
    
    
    Private Function AddSheet(S As String) As Worksheet
        On Error Resume Next
        Application.DisplayAlerts = False
        Worksheets(S).Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
        
        Call ThisWorkbook.Worksheets.Add(MainData)
        ActiveSheet.Name = S
        Set AddSheet = ActiveSheet
    
    
    End Function
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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