Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners -
https://partner.microsoft.com/global/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
The
msoButtonUp and
msoButtonDown states of a menu item can be used to show or hide a check mark. To add a new custom menu to a worksheet and enable it to display a check mark when selected, follow these steps:
- Start Microsoft Excel and open a new workbook.
- On the Tools menu, point to Macro, and then click Visual Basic Editor.
- On the Insert menu, click Module.
- Type or paste the following code:
Sub checked_menuitem() 'create a new menu item
'add a new docked CommandBar
Set mybar = CommandBars.Add(Name:="my command bar", Position:=msoBarTop)
mybar.Visible = True
'add a menu to the custom CommandBar
Set mypopup = mybar.Controls.Add(Type:=msoControlPopup)
mypopup.Caption = "my menu"
'add a menu item to the menu just added to the CommandBar
Set myitem = mypopup.Controls.Add(Type:=msoControlButton)
myitem.Caption = "my menu item"
myitem.OnAction = "check_item"
End Sub
Sub check_item() 'show or hide check mark and perform other actions
Set mypopup = CommandBars("my command bar").Controls("my menu")
If mypopup.Controls("my menu item").State = msoButtonDown Then
'remove check next to menu item
mypopup.Controls("my menu item").State = msoButtonUp
MsgBox "menu item is now unchecked"
Else
'add check next to menu item
mypopup.Controls("my menu item").State = msoButtonDown
MsgBox "menu item is now checked"
End If
End Sub
- Run the checked_menuitem macro.
This macro creates a docked CommandBar with a single menu named "my menu." - Return to Excel through the taskbar or by pressing ALT+F11.
- Click the my menu drop-down list, and then click my menu item.
Notice the message box stating that the menu is now selected,
as well as the check mark next to my menu item. - Repeat step 7 and notice the appearance of the message box and menu item.
Removing the Custom Command Bar
- On the Tools menu, click Customize.
- On the Toolbars tab, scroll through the list of toolbars, and then select the my command bar item.
- Click Delete, and then click OK.
- Click Close.