Solved

How can I get pivot childfield value from Excel VBA

Posted on 2014-12-12
2
171 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 48

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

706 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

15 Experts available now in Live!

Get 1:1 Help Now