您现在的位置是:首页 > Excel技巧>EXCEL创建右键自定义菜单

如何自定义右键菜单-EXCEL创建右键自定义菜单

发布于2022-04-150人已围观

有些时候,我们希望能通过快捷菜单直接调用一些功能。比如右键菜单就能单击右键-选择相应的功能。那么如何自定义右键菜单呢。

如何自定义右键菜单

这里我们用的是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内置图标,如下图中的红色框内的图标。

如何自定义右键菜单


相关文章

文章评论

表情

共0条评论
  • 这篇文章还没有收到评论,赶紧来抢沙发吧~

标签云

站长特荐