View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Leith Ross[_662_] Leith Ross[_662_] is offline
external usenet poster
 
Posts: 1
Default Add menu item to right click


Hello Dev,

Copy this code and paste it into a new VBA Module in your Workbook. Run
it once to add your menu item to the Right-Click menu. Besure to change
MyMacro and MyMenuCaption to what you want.


Code:
--------------------

Public Sub AddToContextMenu()

Dim C
Dim cmdNew As CommandBarButton
Dim MyMenuCaption As String

MyMenuCaption = "Call MyMacro" '<<<< Change this

'Don't add the Menu if it exists.
For Each C In Excel.CommandBars("cell").Controls
If C.Caption = MyMenuCaption Then
Exit Sub
End If
Next C

Set cmdNew = Excel.CommandBars("cell").Controls.Add
With cmdNew
.Caption = MyMenuCaption
.OnAction = "MyMacro name" '<<< Insert the name of your Macro
.BeginGroup = True
End With

End Sub

Public Sub RemoveFromContextMenu()

Dim MyMenuCaption As String

On Error Resume Next
MyMenuCaption = "Call MyMacro" '<<<< Change this
With CommandBars("cell").Controls(MyMenuCaption)
.BeginGroup = False
.Delete
End With

End Sub

--------------------


Sincerely,
Leith Ross


--
Leith Ross
------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=561910