Pivot Table - DrillDown

anastasia

New Member
Joined
Feb 24, 2006
Messages
2
Evening

Hoping someone can point me in the right direction.

I have my source data in "Source"
I have my pivot table in "Pivot"
I have created a separate worksheet called "DrillDown"

When I double click on a cell in "Pivot", is it possible to display the drilldown data in "DrillDown"?
Excel seems to populate it in a new worksheet every time a user drills down. **** annoying to delete each worksheet after looking at the data!

Please help!

Thanks
Anastasia[/b]
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
If your worksheets are established exactly as you say (exactly means exactly, such as the "DrillDown" sheet tab being spelled that way you said, and not "Drill Down" with a space), then place this in your workbook module and see if it accomplishes what you are after. To easily access your workbook module, find the little Excel workbook icon near the upper left corner of your workbook window, usually just to the left of the File menu option. Right click on that icon, left click on View Code, and paste the following procedures into the large white area that is the workbook module:
Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If CS <> "" Then
With Application
.ScreenUpdating = False
Dim NR&
With Sheets("DrillDown")
If WorksheetFunction.CountA(.Rows(1)) = 0 Then
NR = 1
Else
NR = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 2
End If
Range("A1").CurrentRegion.Copy .Cells(NR, 1)
End With
.DisplayAlerts = False
ActiveSheet.Delete
.DisplayAlerts = True
Sheets(CS).Select
.ScreenUpdating = True
End With
End If
End Sub

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
If ActiveSheet.Name = "Pivot" Then
CS = "Pivot"
ElseIf ActiveSheet.Name = "DrillDown" Then
If Not IsEmpty(Target) Then
If Target.Row > Range("A1").CurrentRegion.Rows.Count + 1 _
Or Target.CurrentRegion.Cells(1, 1).Address = "$A$1" Then
Cancel = True
With Target.CurrentRegion
.Resize(.Rows.Count + 1).EntireRow.Delete
End With
End If
End If
End If
End Sub


Then, while in the VBE, click on Insert > Module and paste this into that new module:
Code:
Public CS$




Press Alt+Q to return to the worksheet.


Now, on your "Pivot" sheet, double click in the Data section, and the drill-down records will be placed on the next available row in the DrillDown sheet. It will be best if you clear the DrillDown sheet of all drilled data sets and start fresh.


Four notes:

(1)
This only applies to double-clicking in the data section of the pivot table, because as opposed to the pivot table's row and column header fields, the data section is what generates the insertion of a new sheet when double-clicked. Now, no new sheet will be added, which is the purpose of your request.

(2)
You can double-click the data section to your heart's content and the drill-down records will stack themselves in the DrillDown sheet.

(3)
As an extra feature for you, I included code such that if you want to quickly delete a drill down record, go to the DrillDown sheet and double click in any cell in any record, and that record will be deleted, shifting all drill-down recordsets up.

(4)
The code renders null the ability to insert a new sheet.
 
Last edited by a moderator:
Upvote 0
Hi Tom

Thank you so much! It worked a treat - I especially liked the delete routine in the DrillDown sheet to delete the contents.

You have saved me frustration and time!

Cheers
Anastasia
 
Upvote 0
Re: Pivot Table - DrillDown - possibly one for VBA gurus

Hey Tom.
Cool functionality, work like a charm, many thanks. How can I create a new sheet if needed? (I found that I could copy an existing worksheet but insert is disabled as you point out)
 
Upvote 0
Re: Pivot Table - DrillDown - possibly one for VBA gurus

Thanks for the code above, had to hack it about a bit to get it work for some reason....

But anyway, could anybody tell me how i pick up the Column and Row "criteria" for the cell?

i.e. i want to put a label above the data in drill down that tells the user what they are looking at.

I could obviously do it long winded way but wondered if there was an activecell.pivottable.column type of thing?
 
Upvote 0
Re: Pivot Table - DrillDown - possibly one for VBA gurus

Just thought i'd add this to the thread, i've added some code to bring through the Row, Column and Sheet names. This way when a user has bought several data sets through to Drilldown they can still remember which was which.

Code:
Public Sub Workbook_NewSheet(ByVal Sh As Object)

Dim y(1 To 3) As Integer


If ActiveSheet.Name <> "" Then

    
    With Application
        .ScreenUpdating = False
        Dim NR
        
        With Sheets("DrillDown")
            If WorksheetFunction.CountA(.Rows(1)) = 0 Then
                NR = 1
            Else
                NR = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 2
            End If
            Range("A1").CurrentRegion.Copy .Cells(NR, 1)
        End With
        
        .DisplayAlerts = False
        ActiveSheet.Delete
        .DisplayAlerts = True
        
        x = "Column " & Cells(4, ActiveCell.Column).Value
        y(1) = Len(x)
        
        x = x & " / Row " & Cells(ActiveCell.Row, 1)
        y(2) = Len(x)
         
        x = x & " from Sheet " & ActiveSheet.Name
        y(3) = Len(x)
        
        Sheets("Drilldown").Select
        
        Cells(NR, 1).EntireRow.Select
        Selection.Insert Shift:=xlDown
        ActiveCell.Value = x
        
        With ActiveCell.Characters(Start:=1, Length:=y(1)).Font
            .Name = "Arial"
            .FontStyle = "Bold Italic"
            .Size = 12
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .Color = -16776961
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        
        With ActiveCell.Characters(Start:=y(1) + 1, Length:=y(2) - y(1)).Font
            .Name = "Arial"
            .FontStyle = "Bold Italic"
            .Size = 12
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .Color = -4165632
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        
        With ActiveCell.Characters(Start:=y(2) + 1, Length:=y(3) - y(2)).Font
            .Name = "Arial"
            .FontStyle = "Bold Italic"
            .Size = 12
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .Color = -11489280
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        
                
               
        .ScreenUpdating = True
        End With

End If



End Sub

Oh and this to the delete bit, some of my users wanted to be able to double click into a cell to copy part of the contents out....

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

If ActiveCell.Value <> "" Then

wscancel = MsgBox("Delete this detailed data?", vbYesNo)

If wscancel = 7 Then
    
    Exit Sub
End If

With Target.CurrentRegion
  .Resize(.Rows.Count + 1).EntireRow.Delete
End With

End If

Selection.End(xlUp).Select

End Sub
 
Last edited:
Upvote 0
Re: Pivot Table - DrillDown - possibly one for VBA gurus

In fact put this in place and you get rid of the dodgy activecell bit which would go wrong if the worksheet structure changed, i reference the row and column numbers absolutely.


Code:
.DisplayAlerts = False
        ActiveSheet.Delete
        .DisplayAlerts = True
        
        col_crit = ActiveCell.PivotCell.ColumnItems.Item(1)
        col_crit2 = ActiveCell.PivotCell.RowItems.Item(1)
        
        x = "Column " & col_crit 'Cells(4, ActiveCell.Column).Value
        y(1) = Len(x)
        
        x = x & " / Row " & col_crit2 'Cells(ActiveCell.Row, 1)
        y(2) = Len(x)
         
        x = x & " from Sheet " & ActiveSheet.Name
        y(3) = Len(x)
 
Upvote 0
Re: Pivot Table - DrillDown - possibly one for VBA gurus

Hey guys,

I've used the first set of code and it worked until recently I changed the structure of workbook and added some more tabs and pivots. Well it does not work. I tried to use the code that Wesimmo added but I can't get it to work. Here is what I have, can someone point me in the right direction?

Code:
Option Explicit
Public Sub Workbook_NewSheet(ByVal Sh As Object)
Dim y(1 To 3) As Integer

If ActiveSheet.Name <> "" Then
    
    With Application
        .ScreenUpdating = False
        Dim NR
        
        With Sheets("Loan Level Detail")
            If WorksheetFunction.CountA(.Rows(1)) = 0 Then
                NR = 1
            Else
                NR = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 2
            End If
            Range("A1").CurrentRegion.Copy .Cells(NR, 1)
        End With
        
        .DisplayAlerts = False
        ActiveSheet.Delete
        .DisplayAlerts = True
        
        col_crit = ActiveCell.PivotCell.ColumnItems.Item(1)
        col_crit2 = ActiveCell.PivotCell.RowItems.Item(1)
        
        x = "Column " & col_crit 'Cells(4, ActiveCell.Column).Value
        y(1) = Len(x)
        
        x = x & " / Row " & col_crit2 'Cells(ActiveCell.Row, 1)
        y(2) = Len(x)
         
        x = x & " from Sheet " & ActiveSheet.Name
        y(3) = Len(x)
        
        With ActiveCell.Characters(Start:=1, Length:=y(1)).Font
            .Name = "Arial"
            .FontStyle = "Bold Italic"
            .Size = 12
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .Color = -16776961
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        
        With ActiveCell.Characters(Start:=y(1) + 1, Length:=y(2) - y(1)).Font
            .Name = "Arial"
            .FontStyle = "Bold Italic"
            .Size = 12
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .Color = -4165632
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        
        With ActiveCell.Characters(Start:=y(2) + 1, Length:=y(3) - y(2)).Font
            .Name = "Arial"
            .FontStyle = "Bold Italic"
            .Size = 12
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .Color = -11489280
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        
                
               
        .ScreenUpdating = True
        End With
        
End If
 
End Sub

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If ActiveCell.Value <> "" Then
wscancel = MsgBox("Delete this detailed data?", vbYesNo)
If wscancel = 7 Then
    
    Exit Sub
End If
With Target.CurrentRegion
  .Resize(.Rows.Count + 1).EntireRow.Delete
End With
End If
Selection.End(xlUp).Select
End Sub


Thanks
 
Upvote 0
Re: Pivot Table - DrillDown - possibly one for VBA gurus

I like the code above however I have two separate pivot tables that I want to drilldown and link to separate workbook sheets. How would I do this as I can only use "Private Sub Workbook_NewSheet" & "Private Sub Workbook_SheetBeforeDoubleClick" once in the "ThisWorkbook" module. Basically what I want to do is replicate the DoubleDrill down twice in one workbook with 2 piviot tables which out put to 2 individual sheets. Please can you help. Many Thanks John
 
Upvote 0
Re: Pivot Table - DrillDown - possibly one for VBA gurus

If you look at the code and find the line...

ActiveSheet.Delete

Just after that then you can use an IF statement to change the target sheet to one of the 2 you want it to go to dependent on the sheet you double clicked in.

How the code works is that Excel still creates a new sheet when you double click the pivot, it then copies the data that is put there and then deletes the new sheet before putting it in the Drilldown sheet.

However when it deletes the new sheet Excel it then by default returns to the sheet it was in previously, i.e. the one you double clicked in.

Therefore by checking which sheet you are in after the new sheet has been deleted then you know where you want to put the data.

Something like...

Code:
wsSheetname = Activesheet.name

If wsSheetname = "Sheet Choice 1" Then

    wsDrilldown = "Drilldown 1"

Else 
    
     If wsSheetname = "Sheet Choice 2" then

          wsDrilldown = "Drilldown 2"

     End if

End If

Then later in the existing code, change any reference to "Drilldown" to wsDrilldown (variable so no speech marks).

Hope that makes sense?
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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