• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Populate a ListBox with unique items from PivotTable filed labels

dada rara

New Member
Please help
the bellow code is not working.
(don't have a lot of experience in VBA, trying to adapt googled code to my needs)


I have a pivot table from the data in DataModel.
pivot.jpg

All I need is to populate a ListBox with the list of labels that correspond to the specific date I am looking for (TDATE in the code)
ex. a list for the date 06/11/2015 would be : AWAY,ENDP,FB.TRIP,TSLA,VRX

The setting of the rng is not working
get error "unable to get PivotItems property of the PivotField Class"


Code:
Dim Pvt As PivotTable
Dim Datefld As Variant
Dim Tickernamefld As Variant
Dim rng As Range

Set Pvt = Worksheets("Analysis").PivotTables("PivotTable3")
Tickernamefld = "[Tickets].[Secsym].[Secsym]"
Datefld = "[Tickets].[DateofTrade].[DateofTrade]"

TDATE = CLng(Range("InvestigationDate"))   'get the date to filter data from range name in Excel

Set rng = Intersect(pvt.PivotFields(Datefld).PivotItems(TDATE).DataRange.EntireRow, pvt.PivotFields(Tickernamefld).DataRange.EntireRow)

Worksheets("Analysis").ListBox2.Clear
Worksheets("Analysis").ListBox2.List = Application.Transpose(rng)
 
Code:
pvt.PivotFields(Datefld).PivotItems(TDATE)
will not work. It is very difficult to access date pivot items directly - it is much easier to loop through the items and check if the caption matches the date you want.
 
Back
Top