Search code examples
excelvbamenu

Check if specific Menu Item exist in the Mouse Right Click Menu


The following code adds Menu Items to the mouse right click menu successfully.

Sub AddMenuItemsToTheMouseRightClickMenu()
Application.ShortcutMenus(xlWorksheetCell).MenuItems.AddMenu Caption:=("FIRST MENU")
Application.ShortcutMenus(xlWorksheetCell).MenuItems.AddMenu Caption:=("SECOND MENU")
Application.ShortcutMenus(xlWorksheetCell).MenuItems.AddMenu Caption:=("THIRD MENU")
End Sub

I need a vba code which checks if specific Menu Item exists in the mouse right click menu.

So, the following code needs to be repaired.

Sub CheckIfSpecificMenuItemExistInTheMouseRightClickMenu()
    If Application.ShortcutMenus(xlWorksheetCell).MenuItems("SECOND MENU").Count > 0 Then
        MsgBox "Yes, exist"
    End If
End Sub

I did some internet searches but there is no solution.


Solution

  • Instead of using COUNT, try using INDEX instead:

    If Application.ShortcutMenus(xlWorksheetCell).MenuItems("SECOND MENU").Index > 0 Then
        MsgBox "Yes, exist"
    End If
    

    You will need to add an error handler for when it doesn't exist, however.