Add menus and menu item buttons in Excel through VBA (added when Excel starts)

Source: Internet
Author: User

Tag:vba   excel    add menu    

Save the following code to the. Xlam or. XLA (excel97-2003) file. In the ThisWorkbook object, add the Workbook_Open event, which invokes the Start menu procedure. Private Sub Workbook_Open () call Menusetup (True) End Sub '-----------------------------------------------' Add menus and menu item buttons in Excel (add when Excel starts) '-----------------------------------------------public Function menusetup (blsetup As Boolean) Dim myMenu As CommandBarPopup Dim mycontrol As CommandBarControl Dim i as Integer Dim Smenuitemnam e As String ' Name of the menu item Dim smenuitemfunc As String ' The function name of the call of the "Dim StrM As String ' MenuBar name Dim st Rmenuitem () As String ' menu item name on Error Resume Next ' Initialize menu item ReDim strmenuitem (3, 2) ' VBA array lower bound from 1 start ' menu Item 1 Strmenuitem (1, 1) = "menu item 1" Strmenuitem (1, 2) = "Menu 1 run Procedure name" ' Menu item 2 strmenuitem (2, 1) = "menu item 2" Strmenuitem ( 2, 2) = "Menu 2 run procedure name" application.screenupdating = False '---add menu 1 StrM = "EBS Kit" Set MyMenu = Applica tion. CommandBars (1).    Controls (StrM) ' Determine if my menu exists?       If ERR then Err.Clear Set MyMenu = Application.CommandBars (1).            Controls.Add (Type:=msocontrolpopup, temporary:=true) mymenu.caption = StrM End if Blsetup then '---Add menu item 1 for i = 1 to UBound (strmenuitem) ' array the size of the first dimension smenuitemname = Strmenuitem (i, 1 ) Smenuitemfunc = Strmenuitem (i, 2) Set mycontrol = Mymenu.controls (smenuite Mname) ' Determine if the subroutine exists if Err then err.clear Set mycontrol = Mymenu.cont Rols. Add (Type:=msocontrolbutton, temporary:=true) ' adds a button with mycontrol in the last position of the menu bar. Caption = Smenuitemname ' menu item display name. OnAction = Smenuitemfunc ' Left-click the menu item button to run the procedure. Style = msoButtonCaption ' only display text end With end If Next Els e Application.CommandBars (1). Controls (STRT).        Delete   End if application.screenupdating = True if Err then Err.clearend functionpublic Sub start_app () Frmsetfilesh Eet. Show 0End Sub

Add menus and menu item buttons in Excel through VBA (added when Excel starts)

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.