Home » excel » How can you control what happens when you double-click a Pivot Table Entry in Excel

How can you control what happens when you double-click a Pivot Table Entry in Excel

Posted by: admin April 23, 2020 Leave a comment

Questions:

Is it possible to intercept the double-click on a pivot table, prevent it from showing the underlying data, so that you can provide a structured screen as you wish the viewer to see it?

How to&Answers:

Try this.

LOGIC:

  1. Identify your pivot range
  2. In the Worksheet_BeforeDoubleClick event check if the user clicked in the Pivot
  3. Cancel the double click

CODE: This code goes in the worksheet code area which has the pivot (See screenshot)

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim rng As Range

    '~~> This is your pivot table range. Change as applicable
    Set rng = Range("A1:D10")

    '~~> Check if the double click happend in the Pivot
    If Not Intersect(Target, rng) Is Nothing Then
        '~~> Cancel Double click
        Cancel = True
    End If
End Sub

SCREENSHOT:

enter image description here

Answer:

You can try doing something similar to the code below. This creates a Range, WorkSheet and PivotTable:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim rng As Range
    Dim wks As Worksheet
    Dim pt As PivotTable

    Set wks = Target.Worksheet
    For Each pt In wks.PivotTables()
        Set rng = Range(pt.TableRange1.Address)
        If Not Intersect(Target, rng) Is Nothing Then


            Cancel = True
        End If
    Next
End Sub

Answer:

This is entirely stolen from based on Siddharth Rout’s answer, but will be more flexible, as it will look through all Pivot Tables on a sheet and cancel if you double click in any of them :

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim rng As Range
    Dim wks As Worksheet
    Dim pt As PivotTable

    Set wks = Target.Worksheet
    For Each pt In wks.PivotTables()
        Set rng = Range(pt.TableRange1.Address)
        If Not Intersect(Target, rng) Is Nothing Then


            Cancel = True
        End If
    Next
End Sub