Excel VBA向右键单击菜单添加“将特殊值粘贴”值图标(单元格上下文菜单自定义)

时间:2014-06-05 09:43:33

标签: excel vba excel-vba excel-formula excel-2007

我想知道如何使用VBA代码向右键菜单添加新图标,这将是粘贴特殊值 Excel功能的快捷方式(可以在Excel 2010中找到,但不能在2007年找到) 。添加图标本身不是问题所在,但是是否有可以与该图标关联的通用方法(不是VBA宏)?

下面添加与ToggleCaseMacro宏相关联的图标的代码(此处未定义):

Sub AddToCellMenu()
    Dim ContextMenu As CommandBar
    Dim MySubMenu As CommandBarControl

    ' Delete the controls first to avoid duplicates.
    Call DeleteFromCellMenu

    ' Set ContextMenu to the Cell context menu.
    Set ContextMenu = Application.CommandBars("Cell")

    ' Add one built-in button(Save = 3) to the Cell context menu.
    ContextMenu.Controls.Add Type:=msoControlButton, ID:=3, before:=1

    ' Add one custom button to the Cell context menu.
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=2)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "ToggleCaseMacro"
        .FaceId = 59
        .Caption = "Toggle Case Upper/Lower/Proper"
        .Tag = "My_Cell_Control_Tag"
    End With
End Sub

MSDN page with above code

1 个答案:

答案 0 :(得分:1)

你只需要添加:

ContextMenu.Controls.Add Type:=msoControlButton, ID:=370, Before:=2