Using VBA to get drill down data from a pivot table

Status
Not open for further replies.

MikexcelUK

New Member
Joined
Feb 11, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I am OK working with some VBA, so I can usually squash together different code to get what I need. I am no way an expert and usually rely on forums to find code that does what I need. I have searched for a solution to this and cant find anything...

I have a table of customer data (different utilities, usage, and amount some other fields) that is added to on a monthly basis. I edit the data through a pivot table and then using slicers which allow a particular customer, year and the month to be selected to return that month’s results (using getpivotdata formulas) - the selection of the month/year is to check prior months and trends. There are many different outputs I need, and I can get all of these using the getpivotdata formulas linked to cells (to make them dynamic) and put these into graphs and trends for people in my team to use to populate reports easily. These reports are on a linked worksheet, to the Pivot Table and working sheets are hidden.

The outputs have several fields which are volatile per customer (some may have more or less data (or none) each month). As a result, the pivot table size changes per customer selection (plus it gets wider each month).

Here is what I need:

What I would also like to be able to do is provide that month's underlying data for the chosen customer, plus the data per individual field, if/when that is needed for analysis.

Example: On the report page that is visible, I would have a button to download that month’s power data, another for the water data, one for the combined data, etc. All the drill down data can be selected by double clicking the appropriate cell in the Pivot Table. I can't record anything, because the recorded macro just refers to a cell reference, which changes with each customer selection and month (I only want this for the most recent month).

My thoughts are that I can have control cells automatically update with information pertaining to the pivot table headers and then have the code look at those cells to determine the drill down locations on the pivot table and then to extract that into a new sheet as usual.

I have searched high and low and cannot find anything to help… any ideas? Is this even possible?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Can anyone help please?

I have added an example sheet with Raw data and a Pivot Table. I've highlighted two cells on the PT and the corresponding coloured drill down tabs that are generated by double clicking the PT cell values.

Can this drill down be done with VBA?

If so, what would the code be to select various sources to drill down, based on other criteria?

Thank you VERY much for any advice!!!
 

Attachments

  • Raw.PNG
    Raw.PNG
    83.9 KB · Views: 130
  • Pivot.PNG
    Pivot.PNG
    43.7 KB · Views: 128
  • DD1.PNG
    DD1.PNG
    41.5 KB · Views: 123
  • DD2.PNG
    DD2.PNG
    59.2 KB · Views: 107
Upvote 0
can you add the sourcetable (10-20 lines) with the XL2BB-tool ?
That works easier then a screenshot.

You want a kind of automatic drill down for for example type1, open, yes, moderate, dec 21 and prhaps the customer name on top ?
Every drill down creates a new sheet. Do you want that sheet to be copied to an existing one and the newly created sheet to be deleted ?
 
Upvote 0
Hi there BSALV,

Thank you for replying.

I think I have done this correctly (I hope anyway).

This is my Raw Data:

Book1
ABCDEFGHI
1Customer NameTypeSPAMAutoReferencetask.sys_created_onBreachtask.prioritytask.sys_class_name
2Customer AOpenYesRef294715########TRUE3Type 1
3Customer AOpenYesRef17516########FALSE1Type 1
4Customer AOpenYesRef294719########TRUE3Type 1
5Customer AOpenYesRef294713########TRUE3Type 1
6Customer AOpenYesRef294711########TRUE3Type 1
7Customer AOpenYesRef294716########TRUE3Type 1
8Customer AOpenYesRef293835########FALSE3Type 1
9Customer AOpenYesRef294714########TRUE3Type 1
10Customer COpenRef342616########FALSE4Type 2
11Customer AOpenYesRef293832########FALSE3Type 1
12Customer AOpenYesRef294717########TRUE3Type 1
13Customer BOpenYesRef291222########FALSE3Type 1
14Customer AOpenYesRef293833########FALSE3Type 1
15Customer AOpenYesRef293830########FALSE3Type 1
16Customer AOpenYesRef294718########TRUE3Type 1
17Customer AOpenYesRef294712########TRUE3Type 1
18Customer AOpenYesRef293829########FALSE3Type 1
19Customer AOpenYesRef106361########FALSE2Type 1
20Customer AOpenYesRef106382########FALSE2Type 1
21Customer BOpenYesRef101263########FALSE2Type 1
22Customer BOpenYesRef101261########FALSE2Type 1
23Customer BOpenYesRef101262########FALSE2Type 1
24Customer BOpenYesRef101264########FALSE2Type 1
25Customer BOpenYesRef101286########FALSE2Type 1
26Customer BOpenYesRef291253########FALSE3Type 1
27Customer BOpenYesRef291445########FALSE3Type 1
28Customer AOpenYesRef107588########TRUE2Type 1
29Customer AOpenYesRef294049########TRUE3Type 1
30Customer BOpenYesRef101304########FALSE2Type 1
31Customer BOpenYesRef101296########FALSE2Type 1
32Customer BOpenYesRef101294########FALSE2Type 1
33Customer BOpenYesRef101306########FALSE2Type 1
34Customer BOpenYesRef291252########FALSE3Type 1
35Customer BOpenYesRef101279########FALSE2Type 1
36Customer BOpenYesRef101278########FALSE2Type 1
37Customer BOpenYesRef101265########FALSE2Type 1
38Customer BOpenRef157178########FALSE3Type 1
39Customer COpenRef340754########FALSE4Type 2
40Customer AOpenYesRef106386########FALSE2Type 1
41Customer BOpenYesRef291218########FALSE3Type 1
42Customer AOpenYesRef294727########TRUE3Type 1
43Customer BOpenYesRef291246########FALSE3Type 1
44Customer BOpenYesRef104311########FALSE2Type 1
45Customer BOpenSpamYesRef121124########TRUE2Type 1
46Customer AOpenYesRef293827########FALSE3Type 1
47Customer AOpenYesRef293828########FALSE3Type 1
48Customer BOpenYesRef291227########FALSE3Type 1
49Customer BOpenYesRef101270########FALSE2Type 1
50Customer BOpenYesRef291449########FALSE3Type 1
51Customer AOpenYesRef106384########FALSE2Type 1
52Customer BOpenYesRef106489########TRUE2Type 1
53Customer BOpenYesRef101260########FALSE2Type 1
54Customer AOpenYesRef294729########TRUE3Type 1
55Customer BOpenYesRef293870########TRUE3Type 1
56Customer AOpenYesRef293841########FALSE3Type 1
57Customer AOpenYesRef293844########FALSE3Type 1
58Customer AOpenYesRef17518########FALSE1Type 1
59Customer BOpenYesRef291241########FALSE3Type 1
60Customer AOpenYesRef293849########FALSE3Type 1
61Customer BOpenSpamYesRef121099########FALSE2Type 1
62Customer BOpenYesRef101297########FALSE2Type 1
63Customer AOpenYesRef293964########TRUE3Type 1
64Customer BOpenYesRef291448########FALSE3Type 1
65Customer BOpenYesRef291450########FALSE3Type 1
66Customer BOpenYesRef291231########FALSE3Type 1
67Customer BOpenYesRef104305########FALSE2Type 1
68Customer BOpenYesRef291229########FALSE3Type 1
69Customer BOpenYesRef291230########FALSE3Type 1
70Customer BOpenYesRef101277########FALSE2Type 1
71Customer BOpenYesRef101276########FALSE2Type 1
72Customer BOpenYesRef101275########FALSE2Type 1
73Customer AOpenYesRef293836########FALSE3Type 1
74Customer BOpenYesRef291232########FALSE3Type 1
75Customer BOpenYesRef291228########FALSE3Type 1
76Customer BOpenYesRef104304########FALSE2Type 1
77Customer AOpenYesRef17992########TRUE1Type 1
78Customer AOpenYesRef106372########FALSE2Type 1
79Customer AOpenYesRef106373########FALSE2Type 1
80Customer AOpenYesRef106375########FALSE2Type 1
81Customer AOpenYesRef106377########FALSE2Type 1
82Customer AOpenYesRef106379########FALSE2Type 1
83Customer AOpenYesRef106376########FALSE2Type 1
84Customer AOpenYesRef106374########FALSE2Type 1
85Customer AOpenYesRef106378########FALSE2Type 1
86Customer AOpenYesRef106380########FALSE2Type 1
87Customer AOpenYesRef106371########FALSE2Type 1
88Customer AOpenYesRef291497########FALSE3Type 1
89Customer BOpenYesRef291243########FALSE3Type 1
90Customer AOpenYesRef17539########FALSE1Type 1
91Customer AOpenYesRef17515########FALSE1Type 1
92Customer COpenRef343727########TRUE4Type 2
93Customer BOpenYesRef101287########FALSE2Type 1
94Customer BOpenYesRef101312########FALSE2Type 1
95Customer BOpenYesRef101274########FALSE2Type 1
96Customer COpenRef343725########TRUE4Type 2
97Customer AOpenYesRef291542########FALSE3Type 1
98Customer AOpenYesRef293826########FALSE3Type 1
99Customer AOpenYesRef106391########FALSE2Type 1
100Customer AOpenYesRef17526########FALSE1Type 1
101Customer BOpenYesRef291220########FALSE3Type 1
102Customer AOpenYesRef294724########TRUE3Type 1
103Customer AOpenYesRef17991########TRUE1Type 1
104Customer BOpenYesRef101303########FALSE2Type 1
105Customer BOpenYesRef101310########FALSE2Type 1
106Customer BOpenYesRef101292########FALSE2Type 1
107Customer BOpenYesRef101302########FALSE2Type 1
108Customer BOpenYesRef101295########FALSE2Type 1
109Customer BOpenYesRef101252########FALSE2Type 1
110Customer BOpenYesRef101301########FALSE2Type 1
111Customer BOpenYesRef101289########FALSE2Type 1
112Customer BOpenYesRef101299########FALSE2Type 1
113Customer BOpenYesRef101291########FALSE2Type 1
114Customer BOpenYesRef101305########FALSE2Type 1
115Customer BOpenYesRef101298########FALSE2Type 1
116Customer BOpenYesRef101251########FALSE2Type 1
117Customer BOpenYesRef101288########FALSE2Type 1
118Customer BOpenYesRef101282########FALSE2Type 1
119Customer BOpenYesRef101290########FALSE2Type 1
120Customer BOpenYesRef101300########FALSE2Type 1
121Customer BOpenYesRef101308########FALSE2Type 1
122Customer AOpenYesRef293840########FALSE3Type 1
123Customer COpenRef343210########FALSE4Type 2
124Customer AOpenYesRef293995########TRUE3Type 1
125Customer AOpenYesRef293851########FALSE3Type 1
126Customer AOpenYesRef106389########FALSE2Type 1
127Customer BOpenYesRef291236########FALSE3Type 1
128Customer BOpenYesRef101284########FALSE2Type 1
129Customer BOpenRef340965########FALSE4Type 2
130Customer AOpenYesRef293845########FALSE3Type 1
131Customer COpenYesRef101975########FALSE2Type 1
132Customer BOpenYesRef101309########FALSE2Type 1
133Customer BOpenYesRef101307########FALSE2Type 1
134Customer BOpenRef21203########FALSE2Type 1
135Customer BOpenRef21277########TRUE2Type 1
136Customer AOpenYesRef17996########TRUE1Type 1
137Customer BOpenYesRef104297########FALSE2Type 1
138Customer BOpenSpamYesRef121100########FALSE2Type 1
139Customer COpenYesRef291406########FALSE3Type 1
140Customer BOpenYesRef101271########FALSE2Type 1
141Customer BOpenYesRef106786########TRUE2Type 1
142Customer COpenRef342427########FALSE4Type 2
143Customer AOpenYesRef293848########FALSE3Type 1
144Customer AOpenRef157837########FALSE3Type 1
145Customer BOpenYesRef291239########FALSE3Type 1
146Customer AOpenYesRef106385########FALSE2Type 1
147Customer BOpenYesRef291209########FALSE3Type 1
148Customer BOpenYesRef101254########FALSE2Type 1
149Customer AOpenYesRef293847########FALSE3Type 1
150Customer BOpenYesRef104309########FALSE2Type 1
151Customer BOpenYesRef291240########FALSE3Type 1
152Customer BOpenYesRef291221########FALSE3Type 1
153Customer AOpenYesRef17524########FALSE1Type 1
154Customer AOpenYesRef291548########FALSE3Type 1
155Customer AOpenYesRef293961########TRUE3Type 1
156Customer BOpenYesRef101269########FALSE2Type 1
157Customer AOpenYesRef294721########TRUE3Type 1
158Customer BOpenYesRef291244########FALSE3Type 1
159Customer AOpenYesRef294065########TRUE3Type 1
160Customer AOpenYesRef106383########FALSE2Type 1
161Customer AOpenYesRef17517########FALSE1Type 1
162Customer AOpenYesRef17995########TRUE1Type 1
163Customer COpenRef342613########FALSE4Type 2
164Customer COpenRef342087########FALSE4Type 2
165Customer AOpenYesRef294708########TRUE3Type 1
166Customer BOpenYesRef104302########FALSE2Type 1
167Customer AOpenYesRef293825########FALSE3Type 1
168Customer AOpenYesRef291736########FALSE3Type 1
169Customer BOpenYesRef291444########FALSE3Type 1
170Customer AOpenYesRef17993########TRUE1Type 1
171Customer AOpenYesRef107577########TRUE2Type 1
172Customer AOpenYesRef107570########TRUE2Type 1
173Customer AOpenYesRef107571########TRUE2Type 1
174Customer AOpenYesRef107574########TRUE2Type 1
175Customer AOpenYesRef107576########TRUE2Type 1
176Customer AOpenYesRef107575########TRUE2Type 1
177Customer AOpenYesRef107569########TRUE2Type 1
178Customer AOpenYesRef107572########TRUE2Type 1
179Customer AOpenYesRef107573########TRUE2Type 1
180Customer AOpenYesRef107568########TRUE2Type 1
181Customer AOpenYesRef294000########TRUE3Type 1
182Customer AOpenYesRef294031########TRUE3Type 1
183Customer AOpenYesRef293842########FALSE3Type 1
184Customer BOpenYesRef104298########FALSE2Type 1
185Customer BOpenYesRef291446########FALSE3Type 1
186Customer BOpenYesRef291452########FALSE3Type 1
187Customer BOpenYesRef291454########FALSE3Type 1
Raw


This is my Pivot:

Customer Name(All)
Count of ReferenceColumn Labels
FALSEFALSE TotalTRUETRUE TotalGrand Total
20212021 Total20212021 Total
Row LabelsNovNov
140​
140​
140​
43​
43​
43​
183​
Type 1
133​
133​
133​
41​
41​
41​
174​
Open
133​
133​
133​
41​
41​
41​
174​
3​
3​
3​
1​
1​
1​
4​
2
1​
1​
1​
1​
1​
1​
2​
3
2​
2​
2​
2​
Yes
130​
130​
130​
40​
40​
40​
170​
1
7​
7​
7​
5​
5​
5​
12​
2
71​
71​
71​
13​
13​
13​
84​
3
52​
52​
52​
22​
22​
22​
74​
Type 2
7​
7​
7​
2​
2​
2​
9​
Open
7​
7​
7​
2​
2​
2​
9​
7​
7​
7​
2​
2​
2​
9​
4
7​
7​
7​
2​
2​
2​
9​
Spam
2​
2​
2​
1​
1​
1​
3​
Type 1
2​
2​
2​
1​
1​
1​
3​
Open
2​
2​
2​
1​
1​
1​
3​
Yes
2​
2​
2​
1​
1​
1​
3​
2
2​
2​
2​
1​
1​
1​
3​
Grand Total
142​
142​
142​
44​
44​
44​
186​
 

Attachments

  • Pivot.jpg
    Pivot.jpg
    43 KB · Views: 40
Upvote 0
Essentially what I want to happen is for each drill down to open a new sheet (like it normally would if I double click the value in the PT)... I can then take the new sheets and build a report that extracts itself to a separate workbook (I can do that myself).
 
Upvote 0
i hope you're not a complete rookie with VBA and can work with the macro-recorder.
Suppose you want a certain detail of your pivottable, start the macro-recorder, go somewhere outside the pivottable in a cell, click the "="-key and select that cell in the pivottable, you want the details of and enter.
You stop the macro-recorder and check what you got.
It' rather easy now
- delete the 2nd parameter of that formula, that refers to a cell of your pivottable
- delete every 2nd double quote character
- add a point in front and ".showdetail=true" at the end

Here some examples, from very simple to a little bit more complex.
The names i used here can be different in your situation, because perhaps my pivottable isn't 100% the same.
As you get more experience, in the end you can start using parameters outside that line and then start making macros with parameters, like the last examples


VBA Code:
Sub Test()
     With Sheets("blad1").PivotTables(1)

     '    ActiveCell.FormulaR1C1 = "=+GETPIVOTDATA(""Reference"",R3C16)"
          Set c = .GetPivotData("Reference")                    'the grandtotal
          MsgBox c.Address                                      'just for demo, cell address
          c.ShowDetail = True                                   'show the detail
          ActiveSheet.Name = Format(Now, "hhmmss")              'you can give the new sheet a name and proceed here


     'ActiveCell.FormulaR1C1 = "=+GETPIVOTDATA(""Reference"",R3C16,""SPAM"",)"
          Set c = .GetPivotData("Reference", "SPAM", "Spam")    'subtotal for "Spam"
          MsgBox c.Address                                      'just for demo, cell address
          c.ShowDetail = True                                   'show the detail


     ' "=+GETPIVOTDATA(""Reference"",R3C16,""SPAM"",""Spam"",""task.sys_class_name"",""Type 1"")"
          .GetPivotData("Reference", "SPAM", "Spam", "task.sys_class_name", "Type 1").ShowDetail = True

     '"=+GETPIVOTDATA(""Reference"",R3C16,""SPAM"",,""task.sys_created_on"",2024,""Breach"",""FALSE"",""task.sys_class_name"",""Type 2"")"
          .GetPivotData("Reference", "SPAM", , "task.sys_created_on", 2024, "Breach", "FALSE", "task.sys_class_name", "Type 2").ShowDetail = True

     ' "=+GETPIVOTDATA(""Reference"",R3C16,""SPAM"",""Spam"",""task.sys_created_on"",2022,""Breach"",""FALSE"")"
          myspam = "Spam"                                       'now adding parameters to the "Getpivodata"
          mycreated = 2022
          mybreach = "false"
          .GetPivotData("Reference", "SPAM", myspam, "task.sys_created_on", mycreated, "Breach", mybreach).ShowDetail = True

     '    "=+GETPIVOTDATA(""Reference"",R3C16,""SPAM"",""Spam"",""Auto"",""Yes"",""task.sys_created_on"",2022,""Breach"",""FALSE"",""task.sys_class_name"",""Type 1"")"
          myauto = "yes"
          myclassname = "type1"
          .GetPivotData("Reference", "SPAM", myspam, "Auto", myauto, "task.sys_created_on", mycreated, "Breach", mybreach, "task.sys_class_name", myclassname).ShowDetail = True

     End With
End Sub
 
Upvote 0
Solution
i have it worked out for you, a macro with parameters, once that made, you only have to call him with the right values.
VBA Code:
Sub Within_a_macro()

     'my 3 parameters
     myspam = "Spam"                                            'now adding parameters to the "Getpivodata"
     mycreated = 2022
     mybreach = "false"

     'first make a detail, direct = difficult to do so, without making errors, mistakes
     Sheets("blad1").PivotTables("draaitabel1").GetPivotData("Reference", "SPAM", myspam, "task.sys_created_on", mycreated, "Breach", mybreach).ShowDetail = True

     'second, do the same with a macro with parameters, easy, once done, you only have to provide the right parameters
     ShowDetail_1 Sheets("blad1").PivotTables("draaitabel1"), myspam, mycreated, mybreach

End Sub

Sub ShowDetail_1(PVT As PivotTable, my_spam, my_created, my_breach)
     PVT.GetPivotData("Reference", "SPAM", my_spam, "task.sys_created_on", my_created, "Breach", my_breach).ShowDetail = True
End Sub
 
Upvote 0
I think this is EXACTLY what I am looking for. I had seen the getpivotdata in VBA before, but I thought that was just working the same way as the formula to reference a value. I will work on this in the morning.

THANK YOU VERY MUCH!!!!!!!!!!
 
Upvote 0
I couldn't wait until tomorrow. I have done some initial testing and I totally get it now.

Thank you again so much for your help. This will save me so much time!!!!!!!
 
Upvote 0
Hi Mikexcel ,
can i get the complete working code for this , i am in great need for this...
you can also post the file with code

thanks in Advance.
 
Last edited by a moderator:
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top