VBA Help with custom right click menu.

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, Is it possible to enable a right click custom context menu by Selection?. I can grey out the menus/buttons on workbook open, I have a feeling selection change event will be needed. Can anyone help me with this
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
This is what I have so far but not sure how to make it work,
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

    Dim cbar As CommandBar
    Dim ctrl As CommandBarControl

    If Target.Address.CutCopyMode = xlCopy Or xlCut = True Then
        With cbar
            If .Type = msoBarTypePopup Then
                    If ctrl.Tag = "Custom" Then
    Cancel = True
    Else
    End If
    Cancel False
End Sub
 
Last edited:
Upvote 0
what are you trying to achieve?

This is what I have so far but not sure how to make it work,
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

    Dim cbar As CommandBar
    Dim ctrl As CommandBarControl

    If Target.Address.CutCopyMode = xlCopy Or xlCut = True Then
        With cbar
            If .Type = msoBarTypePopup Then
                    If ctrl.Tag = "Custom" Then
    Cancel = True
    Else
    End If
    Cancel False
End Sub
 
Upvote 0
I am trying to enable a custom right click context menu if the cut or copy functions are used, if not then keep disabled
 
Last edited:
Upvote 0
I am trying to enable a custom right click context menu if the cut or copy functions are used, if not then keep disabled

you need to change the name of myTempCBar to the name of your command bar, from your code above I assume it is a msoBarPopUp type

Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If Not Application.CutCopyMode = False Then
    Cancel = True
    Application.CommandBars("myTempCBar").ShowPopup
End If
End Sub
 
Last edited:
Upvote 0
Yes I am but a bit confused on what you mean by command bar name. My Apologies as I am still learning VBA. I am trying to disable the menu in the code below if the cut or copy functions are used

This Workbook
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If Not Application.CutCopyMode = False Then
    Cancel = True
    Application.CommandBars("myTempCBar").ShowPopup
End If
End Sub

Module
Code:
Option Explicit

Public Const Mname As String = "CustomClearClick"

Sub CustomClearClick()

    Call KillCustomClearClick
    
    Dim ContextMenu1 As CommandBar
    Dim MySubMenu1 As CommandBarControl
    Set ContextMenu1 = Application.CommandBars("Cell")
    
    Set MySubMenu1 = ContextMenu1.Controls.Add(Type:=msoControlPopup, Before:=9)
    

        With MySubMenu1
             .Caption = "&Clear..."
             .Tag = "CustomClear"

        With .Controls.Add(Type:=msoControlButton)
             .OnAction = "ClearAll"
             .Caption = "&Clear All"
             .FaceId = 2060
        End With
        
        With .Controls.Add(Type:=msoControlButton)
             .BeginGroup = True
             .OnAction = "ClearContents"
             .Caption = "&Clear Contents"
             .FaceId = 1964
        End With
        
        With .Controls.Add(Type:=msoControlButton)
             .OnAction = "ClearFormats"
             .Caption = "&Clear Formats"
             .FaceId = 872
        End With
    End With

    ContextMenu1.Controls(9).BeginGroup = True
    
End Sub

Sub KillCustomClearClick()
    On Error Resume Next
    Application.CommandBars(Mname).Delete
    On Error GoTo 0
End Sub

Function ClearAll()
    Selection.Clear
End Function

Function ClearContents()
    Selection.ClearContents
End Function

Function ClearFormats()
    Selection.ClearFormats
End Function
 
Last edited:
Upvote 0
I am trying to disable the menu in the code below if the cut or copy functions are used

Are you trying to disable the entire right click menu if the you are in cutCopyMode?
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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