pivot table vba help

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)


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), _
    End With
    Set Ptbl = Pcach.CreatePivotTable( _
                     TableDestination:=FullRangeName(WsT.Range(TargetCell)), _
                     TableName:=PivotName, _
    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"
    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
        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, _
    End With
End Function

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harry LeeCommented:

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

eastsidemarketAuthor Commented:
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).
Harry LeeCommented:

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

should be

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

eastsidemarketAuthor Commented:
that works, pivottable is a bit off..

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

can you help?
Harry LeeCommented:

  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.
eastsidemarketAuthor Commented:
thanks so much!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.