Excel 2010 VBA 无法显示子菜单

Excel 2010 VBA Can't get Sub Menus to display

我有以下代码在 Excel 中构建自定义菜单。效果很好。我正在尝试修改它以使用子菜单。它将添加 East Options 和 West Options 的菜单项。我正在尝试修改 East 和 West #1 项,使它们显示为子菜单。我尝试了很多不同的东西,但语法不正确。任何帮助,将不胜感激。谢谢.........

Dim cbWsMenuBar As CommandBar
Dim TrCustom As CommandBarControl
Dim iHelpIndex As Long
Dim vFoundMenu As Boolean
Set cbWsMenuBar = Application.CommandBars("Worksheet Menu Bar")

cbWsMenuBar.Visible = True

Dim CCnt As Long
For CCnt = 1 To cbWsMenuBar.Controls.Count
    If InStr(1, cbWsMenuBar.Controls(CCnt).Caption, "Translate") > 0 Then vFoundMenu = True
Next CCnt

If vFoundMenu = False Then

    Set TrCustom = cbWsMenuBar.Controls.Add(Type:=msoControlPopup) ', before:=iHelpIndex)
    With TrCustom

        .Caption = "Menu Items”

        With .Controls.Add(Type:=msoControlButton)
        .Caption = "Business Unit to Group"
        .OnAction = "ShowBU2GP"
        End With

        With .Controls.Add(Type:=msoControlButton)
        .Caption = "Group to Business Unit"
        .OnAction = "ShowGP2BU"
        End With

        With .Controls.Add(Type:=msoControlPopup)
        .Caption = "East Region Options"
        End With

‘       EAST # 1
'        With .Controls.Add(Type:=msoControlButton)
'        .Caption = "East Branch to  DeptID"
'        .OnAction = "ShowEastDeptID"
'        .BeginGroup = True
'        End With

         With .Controls.Add(Type:=msoControlPopup)
        .Caption = "West Options"
        End With

'       WEST # 1
'        With .Controls.Add(Type:=msoControlButton)
'        .Caption = "West Branch to DeptID"
'        .OnAction = "ShowWestDeptID"
'        .BeginGroup = True
'        End With

    End With

End If

我将向您展示一个非常简单的示例。请修改它以满足您的需要:)

Private Sub Sample()
    Dim cb As CommandBar
    Dim cbc As CommandBarControl
    Dim newitem As CommandBarControl
    Dim newSubItem As CommandBarControl

    Set cb = Application.CommandBars(1)

    '~~> Delete Existing command bar control
    On Error Resume Next
    cb.Controls("Menu Items").Delete
    On Error GoTo 0

    '~~> Re Create the Command Bar Control
    Set cbc = cb.Controls.Add(Type:=msoControlPopup, temporary:=False)

    With cbc
        '~~> Main Heading
        .Caption = "Menu Items"

        '~~> First Sub Heading
        Set newitem = .Controls.Add(Type:=msoControlPopup)
        With newitem
            .BeginGroup = True
            .Caption = "East Region Options"
            Set newSubItem = .Controls.Add(Type:=msoControlButton)
            With newSubItem
               .BeginGroup = True
               '~~> Sub Item
               .Caption = "Sub Item for East Region Options"
               .Style = msoButtonCaption
               .OnAction = "SomeMacro"
            End With
        End With

        '~~> Second Sub Heading
        Set newitem = .Controls.Add(Type:=msoControlPopup)
        With newitem
            .BeginGroup = True
            .Caption = "West Region Options"
            Set newSubItem = .Controls.Add(Type:=msoControlButton)
            With newSubItem
               .BeginGroup = True
               '~~> Sub Item
               .Caption = "Sub Item for Est Region Options"
               .Style = msoButtonCaption
               .OnAction = "SomeMacro"
            End With
        End With

        '
        '~~> And So On
        '
    End With
End Sub

截图