Results 1 to 4 of 4

Thread: ** Resolved ** custom right click dropdown menu button

  1. #1

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195

    ** Resolved ** custom right click dropdown menu button

    is this even possible to do? im sure it is possible to add buttons the the right click drop down menu in Excel. i need to have the user right click on a cell and then in the dropdown menu there be a custom button the activate a vba program or macro. thanks for the help in advanced
    Last edited by big_k105; Jul 15th, 2003 at 09:44 AM.

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    The popup CommandBar that shows when you right click a cell is called "Cell". We will add/remove a Control to that CommandBar when the workbook is activated/deactivated so that the custom button only shows in your workbook. The custom button runs a sub in a module.

    In a woorkbook:

    VB Code:
    1. Private Sub Workbook_Activate()
    2.  
    3.     Dim cbc As CommandBarControl
    4.     Dim blnControlFound As Boolean
    5.  
    6.     ' Check if custom control has already been added to Cell popup.
    7.     For Each cbc In Application.CommandBars("Cell").Controls
    8.         If cbc.Caption = "Run My Macro" Then
    9.             blnControlFound = True
    10.             Exit For
    11.         End If
    12.     Next cbc
    13.  
    14.      'Add custom control to Cell popup if not already added.
    15.     If blnControlFound <> True Then
    16.         With Application.CommandBars("Cell").Controls.Add
    17.             .BeginGroup = True          ' Start a new group.
    18.             .FaceId = 346               ' Set display picture.
    19.             .Caption = "Run My Macro"   ' Set display text.
    20.             .OnAction = "MyMacro"       ' Set macro to run.
    21.         End With
    22.     End If
    23.  
    24. End Sub
    25.  
    26. Private Sub Workbook_Deactivate()
    27.  
    28.     ' Delete the custom control From Cell popup menu.
    29.     For Each cbc In Application.CommandBars("Cell").Controls
    30.         If cbc.Caption = "Run My Macro" Then cbc.Delete
    31.     Next cbc
    32.  
    33. End Sub
    In a module:

    VB Code:
    1. Sub MyMacro()
    2.  
    3.     MsgBox "You ran my macro!"
    4.  
    5. End Sub

    Last edited by WorkHorse; Jul 14th, 2003 at 10:55 PM.

  3. #3

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    ok now how to i make it run that code when i right click on a cell. sorry im pretty new to this programming Excell thing.

  4. #4
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Since you marked the topic resolved, I assume you figured out that you don't have to do anything. The custom control is added to the bottom of the normal drop down and Excel does the rest.

    If you want only your wn custom drop down when the user right clicks you could do something like ths:

    VB Code:
    1. Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Excel.Range, Cancel As Boolean)
    2.  
    3.     On Error Resume Next
    4.  
    5.     ' Delet the the Custom popup if it exists.
    6.     Application.CommandBars("Custom").Delete
    7.  
    8.     ' Add the new custom popup.
    9.     With Application.CommandBars.Add("Custom", msoBarPopup)
    10.         With .Controls.Add
    11.             .FaceId = 346               ' Set display picture.
    12.             .Caption = "Run My Macro"   ' Set display text.
    13.             .OnAction = "MyMacro"       ' Set macro to run.
    14.         End With
    15.         .ShowPopup
    16.     End With
    17.  
    18.     Cancel = True
    19.  
    20. End Sub
    Note that this applies to ANY right click, even if it is on a column or row.

Posting Permissions

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



Click Here to Expand Forum to Full Width