这里我们用的是Excel VBA:使用 CommandBarPopup对象用 Controls(index) 可返回一个 CommandBarPopup对象;此处 index 是该控件的索引号。该控件的Type属性必须是 msoControlPopup、msoControlGraphicPopup、msoControlButtonPopup、msoControlSplitButtonPopup 或 msoControlSplitButtonMRUPopup。
详细VBA代码:这段代码保存在Thisworkbook模块即可。Private Sub Workbook_Open() ' Function:自定义右键快捷菜单 ' Author:流沙莫小虫 Dim cmp As CommandBarPopup '定义一个弹出式控件 Application.CommandBars("Cell").Reset '单元格菜单重置 Set cmp = Application.CommandBars("Cell").Controls.Add(Type:=msoControlPopup, before:=1, temporary:=True) ' 添加右键菜单,增加位置第一个 With cmp .Caption = "自定义右键菜单" '菜单命名 ' 下面添加子菜单 With .Controls.Add(Type:=msoControlButton, before:=1) .Caption = "功能1" '子菜单名称 .FaceId = 39 .OnAction = "function1" 'function1是你自定义的功能名称 End With With .Controls.Add(Type:=msoControlButton, before:=2) .Caption = "功能2" .FaceId = 39 .OnAction = "function2" End With With .Controls.Add(Type:=msoControlButton, before:=3) .Caption = "功能3" .FaceId = 39 .OnAction = "function3" End With End With Set cmp = Nothing End Sub
PS:.FaceId是EXCEL内置图标,如下图中的红色框内的图标。