Add custom menus and commands for Excel

Source: Internet
Author: User

The following code was written about three years ago. It seems that it was based on a large piece of code on excelhome.

Put it in the thisworkbook of the xla you need, add a "Custom Command" in your tool menu, and run the program subname when you click this menu:

Const appname as string = "Custom command"

Private sub workbook_addininstall ()
'Call createmenu to add the menu
Createmenu
Msgbox "menu generated to: tool-custom command"
End sub

Private sub workbook_addinuninstall ()
'Call deletemenu to remove the menu
Deletemenu
Msgbox "menu removed: tool-custom command"
End sub

Sub deletemenu ()
Dim xlcommandbar as string
Dim xlmenu as string
Dim xlmenuitem as string
Dim newmenuitem as string

Xlcommandbar = "worksheet menu bar"
Xlmenuitem = ""
Newmenuitem = appname &"..."

Xlmenu = application. commandbars (xlcommandbar). findcontrol (msocontrolpopup, 30007). Caption
On Error resume next
Application. commandbars (xlcommandbar). Controls (xlmenu). Controls (xlmenuitem). Controls (newmenuitem). Delete

Application. commandbars (xlcommandbar). Controls (xlmenu). Controls (newmenuitem). Delete


End sub
Sub createmenu ()

Dim newitem as commandbarbutton

Dim xlcommandbar as string
Dim xlmenu as string
Dim xlmenuitem as string
Dim newmenuitem as string

Xlcommandbar = "worksheet menu bar"
Xlmenu = application. commandbars (xlcommandbar). findcontrol (msocontrolpopup, 30007). Caption 'I am not sure that 30007 this ID is always OK
Xlmenuitem = ""
Newmenuitem = appname &"..."

On Error resume next
Application. commandbars (xlcommandbar). Controls (xlmenu). Controls (xlmenuitem). Controls (newmenuitem). Delete
Application. commandbars (xlcommandbar). Controls (xlmenu). Controls (newmenuitem). Delete

On Error goto 0
If xlmenuitem = "" then
Set newitem = application. commandbars (xlcommandbar). Controls (xlmenu). Controls. Add
Else
Set newitem = application. commandbars (xlcommandbar). Controls (xlmenu). Controls (xlmenuitem). Controls. Add
End if
With newitem
. Caption = newmenuitem
. Onaction = "subname" 'process name triggered by the new menu
. Faceid = 0
. Begingroup = true
End

Exit sub

If err <> 0 then
Msgbox "menu creation error. Please try again", vbinformation, "prompt"
End if
End sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.