Link to home
Start Free TrialLog in
Avatar of eastsidemarket
eastsidemarket

asked on

pivot table vba help

hi,
need some assistance with my vba pivot table.

i want the row labels to be Exchange.
column labels to be Date.
Value to be Qty (but SUM not count)

thanks.


Option Explicit
Option Base 0

Sub VolumeTable()

    Const SourceSheet As String = "pivot test"
    Const PivotName As String = "Lot Report"
    Const TargetCell As String = "A1"
    
    ' Add more columns as required.
    ' Keep "Brkr Name" in first position.
    Const ColumnCaptions As String = "Exchange,Date,Quantity"
    
    Dim WsS As Worksheet                ' Source
    Dim WsT As Worksheet                ' Target (Pivot)
    Dim Prng As Range
    Dim Pcach As PivotCache
    Dim Ptbl As PivotTable
    Dim Pfld As PivotField
    Dim Caps() As String                ' Array(ColumnCaptions)
    Dim i As Long
    
    Caps = Split(ColumnCaptions, ",")
    With ThisWorkbook
        Set WsS = .Worksheets(SourceSheet)
        If Not ColumnsExist(Caps, Prng, WsS) Then Exit Sub
        
        Set Prng = Prng.CurrentRegion
        Set Prng = Prng.Resize(Prng.Rows.Count - 1)
        Set WsT = GetSheet(PivotName)
        Set Pcach = .PivotCaches.Create( _
                     SourceType:=xlDatabase, _
                     SourceData:=FullRangeName(Prng), _
                     Version:=xlPivotTableVersion12)
    End With
    Set Ptbl = Pcach.CreatePivotTable( _
                     TableDestination:=FullRangeName(WsT.Range(TargetCell)), _
                     TableName:=PivotName, _
                     DefaultVersion:=xlPivotTableVersion12)
    
    i = LBound(Caps)
    With Ptbl.PivotFields(Caps(i))
        .Orientation = xlRowField
        .Position = 1
    End With
        
    For i = i + 1 To UBound(Caps)
        Set Pfld = Ptbl.AddDataField(Ptbl.PivotFields(Caps(i)))
        With Pfld
            .Caption = "_" & Caps(i)
            .Function = xlSum
        End With
    Next i
    
    With WsT
        .Range(TargetCell).Value = Caps(0) & "s"
        .Columns.AutoFit
    End With
    ThisWorkbook.ShowPivotTableFieldList = False
End Sub

Private Function ColumnsExist(Caps() As String, _
                              StartCell As Range, _
                              WsS As Worksheet) As Boolean
    Dim i As Long
    
    For i = UBound(Caps) To LBound(Caps) Step -1
        Caps(i) = Trim(Caps(i))
        Set StartCell = WsS.Cells.Find(Caps(i))
        If StartCell Is Nothing Then
            MsgBox "Column '" & Caps(i) & "' doesn't exist" & vbCr & _
                   "in worksheet '" & WsS.Name & "'." & vbCr & _
                   "Sorry, I can't continue.", _
                   vbCritical, "Fatal source data error"
        End If
    Next i
    ColumnsExist = True
End Function

Private Function GetSheet(ByVal Sn As String) As Worksheet

    Dim Idx As Long
    
    DelSheet Sn
    With ThisWorkbook
        Set GetSheet = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
        ActiveSheet.Name = Sn
    End With
End Function

Private Sub DelSheet(ByVal Sn As String)

        On Error Resume Next
        Application.DisplayAlerts = False
        ThisWorkbook.Worksheets(Sn).Delete
        Application.DisplayAlerts = True
End Sub

Private Function FullRangeName(rng As Range) As String
    
    Dim Ln As String
    
    With rng
        Ln = .Parent.Name
        If InStr(Ln, " ") Then Ln = "'" & Ln & "'"
        FullRangeName = Ln & "!" & Application.ConvertFormula( _
                                   Formula:=.Address, _
                                   FromReferenceStyle:=xlA1, _
                                   ToReferenceStyle:=xlR1C1)
    End With
End Function

Open in new window

Avatar of Harry Lee
Harry Lee
Flag of Canada image

eastsidemarket,

After the Pivot Table was created in your code, you have to add the following code in.

I don't see that you renamed your Pivot Table in your attached code. The following code should work if there is only 1 pivot table in the Workbook.

    With ActiveSheet.PivotTables("Lot Report").PivotFields("Exchange")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("Lot Report").PivotFields("Date")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("Lot Report").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Quantity"), "Sum of Quantity", xlSum

Open in new window

Avatar of eastsidemarket
eastsidemarket

ASKER

unable to get the PivotTables property of the worksheet class

    ActiveSheet.PivotTables("Lot Report").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Traded Quantity"), "Sum of Quantity", xlSum

i changed columncaptions above to reflect Traded Quantity (as this it he header in my workbook).
ASKER CERTIFIED SOLUTION
Avatar of Harry Lee
Harry Lee
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that works, pivottable is a bit off..

Exchange (Row Labels), Date (Column Labels), Traded Quantity (Values) – SUM not count                                                

can you help?
eastsidemarket,

  ActiveSheet.PivotTables("Lot Report").AddDataField ActiveSheet.PivotTables( _
        "Lot Report").PivotFields("Traded Quantity"), "Sum of Traded Quantity", xlSum

the bold text in the above code should make the Quantity Value sum but not count.
thanks so much!