Solved

How can I get pivot childfield value from Excel VBA

Posted on 2014-12-12
2
173 Views
Last Modified: 2014-12-14
In the attached file I have some data and a Pivot table.
In the data source column #3 (Sub Project Number) is a value that I do not show, but a value I need.

When selecting a subproject in the Pivot, how do I get the value of the Sub Project NUmber child item using Excel VBA.

Please notice that the column "Sub Project" is not Unique. (10 - XXXXX)

Thank you
C--Users-hhbr-Desktop-Pivot-Example.xlsx
0
Comment
Question by:Hans Henrik Brandt
2 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40496187
Hi,

pls try

Sub Macro()

Set Sel = Selection

On Error Resume Next
Set pvtfld = Sel.PivotField
On Error GoTo 0

If Not IsEmpty(pvtfld) Then
    If Sel.PivotField.Name = "Sub Project" Then
    SubProjectName = Sel.Formula
    Idx = 0
    Do
        Idx = Idx - 1
    Loop Until Sel.Offset(Idx).PivotField.Name <> "Sub Project"
    MainProjectName = Sel.Offset(Idx).Formula
    Result = Evaluate("=INDEX(Table1[Sub Project Number],MATCH( " & Chr(34) & MainProjectName & SubProjectName & Chr(34) & ",Table1[Main Project]&Table1[Sub Project],0))")
    MsgBox "Result: " & Result
    End If
End If

End Sub

Open in new window

Regards
0
 

Author Comment

by:Hans Henrik Brandt
ID: 40499548
Thank you so much :). Did the job perfectly.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now