Solved

Macro to list PivotTable filter selection

Posted on 2013-01-06
4
403 Views
Last Modified: 2013-01-07
Hello,

Can someone please confirm whether or not it would be possible to create a macro that would look at the filtered range in a pivot table dropdown filter and then list in a cell for example "2012 to 2023". I have attached a picture to give an idea of what I mean. Generally with a pivot table filter, once you select more than 1 item it just shows "Multiple Items" whereas I would like to display the lowest and the highest years as a heading.

I have tried using a formula but I can't work it out.

Can anyone please help?

Thank you!!
Example.pdf
0
Comment
Question by:vegas86
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38749706
Yes it is possible. Can you upload a small sample file for "how to do it"?
0
 

Author Comment

by:vegas86
ID: 38749864
Here you go!
example.xlsx
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 38750044
Try this

Right-click on the sheet tab name
select view code
paste this code in the VBA window
close the VBA window
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    Dim pitm As PivotItems, mx As Integer, mn As Integer, i As Integer
    Set pitm = Me.PivotTables(1).PivotFields("Year").PivotItems
    mx = 0
    mn = 9999
    For i = 1 To pitm.Count - 1
        If pitm(i).Visible = True Then
            If Val(pitm(i)) < mn Then mn = Val(pitm(i))
            If Val(pitm(i)) > mx Then mx = Val(pitm(i))
        End If
    Next i
    Range("d2").Value = mn & " - " & mx
End Sub

Open in new window

0
 

Author Closing Comment

by:vegas86
ID: 38750081
FANTASTIC :)

Thank you so much!!!!!
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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