Adding a Macro button to the MenuBare upon opening up a file

mach3

Board Regular
Joined
Mar 24, 2002
Messages
245
i need a button (with macro attached) to show up on a user's menubar upon open/close of the worksheet.

i once wrote similar code where a new menu item showed up on the worksheet menu bar upon open/close. i remember that the code had to be written in the 'ThisWorkbook' section of VB (NOT in a module) and that the code involved a CALL. however, beyond that, i forgot the code.

could someone please offer a quick macro that applies a macro button (w/ macro attached) to a user's menu bar upon Open and deletes the button upon Close? thanks.



mach3
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hiya,

Here's a spot of code from one of my workbooks that adds and removes a button called Select Sample, which in turn runs a macro called StartApp. Hope this does the trick...

Private Sub Workbook_Open()
Dim myButton As CommandBarButton
Set myButton = Application.CommandBars("Worksheet Menu Bar").Controls.Add
myButton.Caption = "&Select Sample"
myButton.Style = msoButtonCaption
myButton.BeginGroup = True
myButton.OnAction = "StartApp"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("Select Sample").Delete
End Sub


P.S. You're right. Put it in Thisworkbook, not the module.
 
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,222
Members
448,877
Latest member
gb24

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