Consulting

Results 1 to 3 of 3

Thread: Pivot Table Grand Total

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Pivot Table Grand Total

    I'm creating a pivot table using the following code, but am not getting a Grand Total column. Any suggestions?
    Regards
    MD




    Sub CostxPivotZones()
        Dim r As Range
        Dim Head As Range
        Dim sh As Worksheet
        Dim i As Long
        Dim Hd As String
    
    
        'Clear old data
        Application.DisplayAlerts = False
        On Error Resume Next
        Sheets("Pivot").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
    
    
        Set r = Cells(1, 1).CurrentRegion
        'Get column headers
        Set Head = Range(Cells(1, 7), Cells(1, Columns.Count).End(xlToLeft))
        Set sh = Sheets.Add
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
                                          r, Version:=xlPivotTableVersion15). _
                                          CreatePivotTable TableDestination:=sh.Cells(3, 1), TableName:="PivotTable1" _
                                        , DefaultVersion:=xlPivotTableVersion15
    
    
    
    
        With sh.PivotTables("PivotTable1")
            'Set row fields
            With .PivotFields("Zone")
                .Orientation = xlRowField
                .Position = 1
            End With
            With .PivotFields("Type")
                .Orientation = xlRowField
                .Position = 2
            End With
            'Set Column Fields
            For i = 1 To Head.Cells.Count
                Hd = Head(i).Value
                .AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields(Hd), " " & Hd, xlSum
            Next i
            
            'Suggested solution but no result
            .ColumnGrand = True
            .RowGrand = True
            '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
            
            
            .TableStyle2 = "PivotStyleLight14"
            .ShowTableStyleColumnStripes = True
        End With
        Cells.NumberFormat = "0.00"
        Rows(3).HorizontalAlignment = xlRight
        Columns("B:Z").Columns.AutoFit
        Application.Goto sh.Cells(3, 1)
        ActiveSheet.Name = "Pivot"
    
    
    End Sub
    Attached Files Attached Files
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Your G-Z are not really column headers, essentially the same as more row headers, so there was nothing to generate a column GT

    Making your data into a list and making a PT manually seems to give the format that I think you were expecting
    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

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Paul
    It greatly simplifies the coding putting the Height categories into the table. The Headers are not required and the Pivot works correctly.
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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