custom menu commands

schultz1414

New Member
Joined
Oct 28, 2002
Messages
17
:unsure: how can i use vba to add a new menu item and attach a macro to it in a workbook, and then remove it after the workbook is closed. i know you use to be able to do this using excel 97, but i am using xp.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Here is code that will create a menu item in the toolbar...


Code:
Private Const strMenu As String = "My Menu"

Private Sub auto_open()
'=========================================
' Author: wongm003
' Purpose: create temporary menu toolbar
'          when workbook opens
'=========================================
On Error Resume Next
    Call create_MENU_BAR
End Sub

Private Sub auto_close()
'=========================================
' Author: wongm003
' Purpose: delete temporary menu toolbar
'          when workbook closes
'=========================================
On Error Resume Next
    Call delete_MENU_BAR
End Sub

Public Sub create_MENU_BAR()
'=========================================
' Author: wongm003
' Purpose: create temporary menu toolbar
'=========================================
On Error Resume Next

    Dim myBar As CommandBar
    
    Call delete_MENU_BAR
    
    Set myBar = CommandBars.Add _
        (Name:=strMenu, Position:=msoBarFloating, Temporary:=True)
    With myBar
        .Visible = True
        .Position = msoBarTop
    End With
    
    Set ctrl1 = myBar.Controls _
        .Add(Type:=msoControlButton)
    With ctrl1
        .Style = msoButtonIconAndCaption
        .FaceId = 1648
        .Caption = "My Macro"
        .TooltipText = "Select to run my macro"
        .OnAction = "sub_name"
    End With
    
End Sub


Public Sub delete_MENU_BAR()
'=========================================
' Author: wongm003
' Purpose: delete cdm toolbar
'=========================================
On Error Resume Next
    CommandBars(strMenu).Delete
End Sub

Public Sub sub_name()
'=========================================
'Your code here
'=========================================
msgbox "This is my macro"

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,721
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top