pivot tables and showdetail/drill down

charlie1105

Board Regular
Joined
Oct 16, 2007
Messages
182
Hi all,

just wondering if anyone knows if it's possible to get a drill down of multiple cells from a pivot table into the same sheet easily, as when I use

Code:
    Range("BE23:BE26").Select
    Selection.ShowDetail = True

it only pulls out the first drill down (i.e. the equivalent of double clicking BE23)

I'm sure there's a work around I can write which performs each drill down separately then combine the sheets, but it's probably going to end up fairly longwinded!

If anyone knows of an easier way, please let me know

Cheers

Charlie
 
@Tom Urtis
If you want to manually highlight code you need to use the <rich/> code tags, the <vba/> automatically colours the code & cannot be overridden.
I have edited your posts to show the difference.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello James - -

I've posted many variations of this topic because over time (this thread goes back 13 years) there have been a mixture of ad hoc requests, which is fine.

So as to your first question, here is the code you need, which will delete drill down records when they are double-clicked upon. NOTE: Be sure to modify the code where I have Sheet1 as the VBA codename sheet as to where the pivot table actually resides.

In the workbook module:

Rich (BB code):
Private Sub Workbook_NewSheet(ByVal Sh As Object)
'Declare a variable for the next available row to stack a recordset.
Dim NextRow As Long

With Application
'Turn off ScreenUpdating.
.ScreenUpdating = False

'Determine the next available row and copy the recordset to it.
With Sheet1
NextRow = _
.Cells.Find(What:="*", After:=.Range("A1"), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 2
Range("A1").CurrentRegion.Copy .Cells(NextRow, 1)
End With

'Delete the active sheet that you will never see, which is the
'sheet that got produced by double-clicking a pivot table cell.
'Set DisplayAlerts to False so you are not prompted to confirm
'the deletion of this new sheet.
.DisplayAlerts = False
ActiveSheet.Delete
'Turn back on DisplayAlerts.
.DisplayAlerts = True

'Turn ScreenUpdating back on.
.ScreenUpdating = True
End With
End Sub


In the worksheet module where you will be double-clicking to delete recordsets:

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

'Turn off ScreenUpdating.
Application.ScreenUpdating = False

'Declare a variable to determine if the cell you double-clicked
' • belongs to a pivot table,
' • or has data in a recordset you want to delete,
' • or is some other cell having nothing to do with pivot tables.
Dim PivotTargetType As Integer
On Error Resume Next
'Is the cell that was double-clicked a pivot table cell?
PivotTargetType = Target.PivotCell.PivotCellType
'If not, clear the error.
If Err.Number = 1004 Then
Err.Clear
'If so, then if there is data in the cell...
If Not IsEmpty(Target) Then
'If the row of the cell that was double-clicked is not in the
'explanatory text at the top of the sheet in this example..
If Target.Row > Range("A1").CurrentRegion.Rows.Count + 1 Then
'Avoid going into Edit mode from the double-click.
Cancel = True
'Release filters that may be present, in order to show all rows.
'The error bypass is if no to-be-deleted recordsets are being filtered.
On Error Resume Next
ActiveSheet.ShowAllData
Err.Clear
'Delete the rows belonging to the CurrentRegion of the double-clicked recordset.
With Target.CurrentRegion
.Resize(.Rows.Count + 1).EntireRow.Delete
End With
End If
End If
End If

'Turn ScreenUpdating back on.
Application.ScreenUpdating = True
End Sub

Note that there is to be no macro in a standard module for this. It's the above 2 events (workbook and worksheet) and that's it as far as the code goes. I just tested it again and am sure it works as advertised.

Regarding your other question, it sounds like a change event tied to the pivot table itself (when you wrote "if a filter (or indeed the contents of the pivot) is applied") can trigger a macro to simply clear the cells below the pivot table if that's what you mean. Certainly do-able but you'd want to have a confirmation message box to first appear, so someone's hard work at creating those dozens of recordsets doesn't go to waste by clearing them all in one fell swoop or any inadvertent change.

Editing this only to call attention to the board's admins regarding how the green font color tags appear when clicking on their icon and palette color as directed in the local menu bar to change a particular text's color, unless I did it wrong which is always a possibility. I did it the same way for this red font color which looks right, with no such visible code tags.
Tom, can’t seem to PM on this site. Can you help ref messages above.eith the original VBA I can get the tables below the pivot(but they won’t delete) and can add the new vba which deletes but then stops the creation of the tables??
 
Upvote 0
Hi Tom,

Thank you for this excellent code, and sorry if this is already answered,

I need your help to exclude some of the rows from being deleted.
So I have the Pivot in row 20 onwards and have some data/info in the first 20 rows however if someone double clicks on the top rows above the pivot they get deleted too. Is there a way to exclude those rows in the code.

Thanks in advance.
 
Upvote 0
The programming code should have avoided that behavior with this snippet I already put into that procedure:

Rich (BB code):
Dim PivotTargetType As Integer
On Error Resume Next
'Is the cell that was double-clicked a pivot table cell?
PivotTargetType = Target.PivotCell.PivotCellType
'If not, clear the error.
If Err.Number = 1004 Then
Err.Clear
'If so, then if there is data in the cell...

Something else is going on in your worksheet, but in any case, see if this works:

Immediately below the procedure name line of...
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
...insert this line:
If Target.Row < 20 Then Exit Sub
 
Upvote 0
The programming code should have avoided that behavior with this snippet I already put into that procedure:

Rich (BB code):
Dim PivotTargetType As Integer
On Error Resume Next
'Is the cell that was double-clicked a pivot table cell?
PivotTargetType = Target.PivotCell.PivotCellType
'If not, clear the error.
If Err.Number = 1004 Then
Err.Clear
'If so, then if there is data in the cell...

Something else is going on in your worksheet, but in any case, see if this works:

Immediately below the procedure name line of...
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
...insert this line:
If Target.Row < 20 Then Exit Sub
Thank You Tom! It worked.
 
Upvote 0
Hi Tom,
thank you for posting this excellent code. I however, cannot seem to get the second part to work (where it deletes the line again). I am using Excel 365 - so maybe because it is a later version? Basically, I have four pivot tables displaying on a summary page. I can expand any line, double click on the value and are presented with a select fields box and when I select what I want , it displays just fine. However, when I then double click on that information (to delete it), excel opens another sheet with that data in it (standard pivot table behaviour) instead of deleting that information as expected.
I can of course, right click, show field list and clear the values, but that's not what I'm after. Are you able to please advise?
thank you.
 

Attachments

  • Capture.PNG
    Capture.PNG
    24.7 KB · Views: 6
Upvote 0
A few things going on that are not a part of what my code examples do under the circumstances of their respective requests.

For one thing, this you wrote...
"can expand any line, double click on the value and are presented with a select fields box"
...is not an intended function of my code.

But what you wrote here...
"However, when I then double click on that information (to delete it), excel opens another sheet with that data in it (standard pivot table behaviour) instead of deleting that information as expected."
...is what should happen in the first line I quoted above, not on a data drilled-down-produced pivot table subset.

It sounds like the behavior of my code is acting upon already-created data subsets, treating them as if they were pivot tables themselves instead of a pivot table's data subset that would be gotten from double-clicking a pivot table data item as was first requested a many years ago on this thread, and what my heretofore code does.

Back then, 365 did not exist but now it does. It sounds like how you are putting several pivot tables on one worksheet, which is nothing you are doing wrong and is totally OK, requires an additional line or two of code to specify where a dataset should go depending on the name each pivot table you have created and are double-clicking on. And further, you would need to specify in the deletion code, maybe – this is where I am guessing because I am not familiar with your workbook – what the sheet name is for any particular dataset and what its parent pivot table name is.

But what you want to achieve is definitely do-able. Programming code has no emotions or sentiments; it simply executes blindly as it is written. I think the code you are using expected a 2016 (or maybe earlier version given how long ago this thread started) or 365 environment, and only needs a nudge or two to accommodate your workbook's design.
 
Upvote 0
Thanks Tom for the reply. I'm not a coder, but can follow instructions! Not really sure then, what I need to amend to achieve the result I'm after which, basically is that an Executive can look at my summary page - which includes the pivot tables (see attached image) and if they require further detail, expand the pivot table, select the relevant information and when done, the pivot table returns to how it was. Default behaviour when you double click any detail is that it opens another worksheet to display the data - which of course is WAY too tech savvy for the Executives to deal with, not only that, but then the sheet they viewed has to be deleted to return the workbook to it's default condition.
What do you need to see to help me facilitate & implement what I'm trying to achieve please?
 

Attachments

  • Capture.PNG
    Capture.PNG
    39.7 KB · Views: 6
Upvote 0
Candidly, I could well be wrong but I think it is reasonable to presume, based on all you have written, that this would be more of a project, albeit a relatively small one as projects go, and not a simple additional question & answer to complete all of what you need. There are too many unknowns, whereby it is probably necessary to find someone familiar enough with VBA to look at your workbook and elicit what all your and (especially) your Executives' full and ultimate intentions are, and then modify my code accordingly.

I am not available so I suggest you search on line for VBA experts to hire for this. But beware to vet them and choose carefully for someone really at the level you need.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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