to deny items of context menu for PivotTable

vics_roo

Board Regular
Joined
Apr 3, 2015
Messages
75
Hi,

I have PivotTable. I need to deny for user the part of context menu (by mouse right click)
Need to deny these items
"Show field list"
"Show Values As"

How I can remove them or set unactive ?

Thanks at advance
Vic
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I did something like that

Code:
Option Explicit

Public Sub New_Commandbar()
Dim Cbr As CommandBar
Dim Ctr As CommandBarControl
On Error Resume Next
Application.CommandBars("PivotTable Context Menu").Delete
Application.CommandBars.Add Name:="PivotTable Context Menu", Position:=msoBarPopup, Temporary:=True
For Each Ctr In Application.CommandBars("PivotTable Context Menu").Controls
    With Application.CommandBars("PivotTable Context Menu").Controls.Add(Ctr.Type, Ctr.ID, Ctr.Parameter, , 1)
        If (Ctr.Caption <> "Show field list") Or (Ctr.Caption <> "Show Values As...") Then
        
        Else
            Ctr.Delete
        End If
    End With
Next
End Sub

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
  Cancel = True
  New_Commandbar
  Application.CommandBars("PivotTable Context Menu").Show
End Sub

Seems it works...but i have a lot of empty items in context menu...and OOOPS it's affect not only Active workbook,but all workbooks with Pivot....:confused:

How to remove empty items ? and do it only for active workbook ?
 
Upvote 0
I fixed some bugs. Now my code is

Code:
Option Explicit

Dim ContextMenu As CommandBar

Public Sub New_Commandbar()
'    Dim Cbr As CommandBar
    Dim Ctr As CommandBarControl
    On Error Resume Next

    
    Application.CommandBars("PivotTable Context Menu").Delete
    Application.CommandBars.Add Name:="PivotTable Context Menu", Position:=msoBarPopup, Temporary:=True
    For Each Ctr In Application.CommandBars("PivotTable Context Menu").Controls
        With Application.CommandBars("PivotTable Context Menu").Controls.Add(Ctr.Type, Ctr.ID, Ctr.Parameter, , 1)
          If (Ctr.Caption <> "Show field list") Or (Ctr.Caption <> "Show Values As...") Then
        
          Else
                Ctr.Delete
          End If
        End With
    Next
    Set ContextMenu = Application.CommandBars("PivotTable Context Menu")
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.CommandBars("PivotTable Context Menu").Reset
End Sub

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
  Cancel = True
  New_Commandbar
  Application.CommandBars("PivotTable Context Menu").Show  ' <-COMPILE ERROR method or data member not found
End Sub

But I have compile error.So how to remove these items from pivot table context menu ?

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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