+ Reply to Thread
Results 1 to 5 of 5

Replace standard shortcut menu with custom menu

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2014
    Location
    Port Elizabeth, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Replace standard shortcut menu with custom menu

    Hi all
    I have created a sub to add new controls to the "Cell" shortcut menu. Can I remove the standard "Cell" shortcut menu controls? if so how do you do it?

    Sub AddToShortCut()
    
    '   Adds move command to shortcutmenu
        Application.CommandBars("Cell").Enabled = False
    
        Dim Bar As CommandBar
        Dim NewControl As CommandBarButton
        ResetAllBar
        Set Bar = Application.CommandBars("Cell")
        
        'Add move to scratch control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=21, _
        temporary:=True)
        With NewControl
            .Caption = "&Move to Scratch"
            .OnAction = "Move_To_Scratch"
            .Style = msoButtonIconAndCaption
        End With
        
        'Add Copy Selection control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=19, _
        temporary:=True)
        With NewControl
            .Caption = "&Copy Selection"
            .OnAction = "Copy1"
            .Style = msoButtonIconAndCaption
        End With
        
        'Add paste selection control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=997, _
        temporary:=True)
        With NewControl
            .Caption = "&Paste selection"
            .OnAction = "Paste1"
            .Style = msoButtonIconAndCaption
        End With
        
        'Add selection control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=374, _
        temporary:=True)
        With NewControl
            .Caption = "&Select Trip"
            .OnAction = "Select_Trip"
            .Style = msoButtonIconAndCaption
        End With
          
        'Add delete control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=9523, _
        temporary:=True)
        With NewControl
            .Caption = "&Delete Trip"
            .OnAction = "Delete1"
            .Style = msoButtonIconAndCaption
        End With
        
        Application.Calculation = xlCalculationManual      'Turn off calculation
      
    End Sub

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Replace standard shortcut menu with custom menu

    Perhaps it will be easier to create own menu:
    Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
        Dim Bar As CommandBar
        Dim NewControl As CommandBarButton
        
        ' remove existing
        On Error Resume Next
        Application.CommandBars("MyCell").Delete
        On Error GoTo 0
        
        ' cancel regular cell menu
        Cancel = True
    
    '   make popup menu
        Set Bar = Application.CommandBars.Add("MyCell", msoBarPopup)
        
        'Add move to scratch control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=21, _
        temporary:=True)
        With NewControl
            .Caption = "&Move to Scratch"
            .OnAction = "Move_To_Scratch"
            .Style = msoButtonIconAndCaption
        End With
        
        'Add Copy Selection control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=19, _
        temporary:=True)
        With NewControl
            .Caption = "&Copy Selection"
            .OnAction = "Copy1"
            .Style = msoButtonIconAndCaption
        End With
        
        'Add paste selection control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=997, _
        temporary:=True)
        With NewControl
            .Caption = "&Paste selection"
            .OnAction = "Paste1"
            .Style = msoButtonIconAndCaption
        End With
        
        'Add selection control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=374, _
        temporary:=True)
        With NewControl
            .Caption = "&Select Trip"
            .OnAction = "Select_Trip"
            .Style = msoButtonIconAndCaption
        End With
          
        'Add delete control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=9523, _
        temporary:=True)
        With NewControl
            .Caption = "&Delete Trip"
            .OnAction = "Delete1"
            .Style = msoButtonIconAndCaption
        End With
        
        Application.Calculation = xlCalculationManual      'Turn off calculation
      
        Bar.ShowPopup
    End Sub
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Registered User
    Join Date
    01-07-2014
    Location
    Port Elizabeth, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Replace standard shortcut menu with custom menu

    Thank you very much it works very well.

    Is there a way to set the position of the pop up menu relative to the active cell? to minimise the mouse movement required. as default the 1st control is at the level of the cursor, I would like the cursor to be midway down the menu.

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Replace standard shortcut menu with custom menu

    You may do like this:
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
        Dim Bar As CommandBar
        Dim NewControl As CommandBarButton
        Dim pt As POINTAPI
        
        ' remove existing
        On Error Resume Next
        Application.CommandBars("MyCell").Delete
        On Error GoTo 0
        
        ' cancel regular cell menu
        Cancel = True
    
    '   make popup menu
        Set Bar = Application.CommandBars.Add("MyCell", msoBarPopup)
        
        'Add move to scratch control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=21, _
        temporary:=True)
        With NewControl
            .Caption = "&Move to Scratch"
            .OnAction = "Move_To_Scratch"
            .Style = msoButtonIconAndCaption
        End With
        
        'Add Copy Selection control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=19, _
        temporary:=True)
        With NewControl
            .Caption = "&Copy Selection"
            .OnAction = "Copy1"
            .Style = msoButtonIconAndCaption
        End With
        
        'Add paste selection control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=997, _
        temporary:=True)
        With NewControl
            .Caption = "&Paste selection"
            .OnAction = "Paste1"
            .Style = msoButtonIconAndCaption
        End With
        
        'Add selection control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=374, _
        temporary:=True)
        With NewControl
            .Caption = "&Select Trip"
            .OnAction = "Select_Trip"
            .Style = msoButtonIconAndCaption
        End With
          
        'Add delete control
        Set NewControl = Bar.Controls.Add _
        (Type:=msoControlButton, ID:=9523, _
        temporary:=True)
        With NewControl
            .Caption = "&Delete Trip"
            .OnAction = "Delete1"
            .Style = msoButtonIconAndCaption
        End With
        
        Application.Calculation = xlCalculationManual      'Turn off calculation
        GetCursorPos pt
        Bar.ShowPopup pt.X, pt.Y - Bar.Height / 2
    End Sub
    However if this will position menu off screen Excel will move it.

  5. #5
    Registered User
    Join Date
    01-07-2014
    Location
    Port Elizabeth, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Replace standard shortcut menu with custom menu

    Thank you much appreciated!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Trying to 'Grey Out' menu items on Custom Menu if Workbook is ReadOnly
    By TC1980 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-30-2013, 08:22 AM
  2. Persistent custom right-click shortcut menu item
    By ahartman in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2009, 01:21 PM
  3. [SOLVED] How to add shortcut to custom menu entry?
    By Benjamin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-24-2006, 07:40 AM
  4. [SOLVED] Add standard excel button to custom toolbar menu
    By Gixxer_J_97 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-21-2005, 03:20 PM
  5. Adding Sub Menu Item to Current Custom Menu
    By Renato in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2005, 08:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1