Assign macro to control button

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,069
I am having trouble with the following script.

I have been trying to assign a macro to it but I just can't work it out.

I want to assign a macro named "Form1" to the control and then when I close my workbook it would delete the button.

I have tried the .onaction script but can't get it to work.

Also can someone provide a script the same but rather than the control button in the script below it would add a button named "Form2" with the macro named "Form1" assigned to it.

Code:
Application.CommandBars("Worksheet Menu Bar").Controls.Add Type:= _
        msoControlButton, ID:=1183

Thank You
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
OK, I have found the following script, the only problem I am having is that everytime I click in a worksheet cell the button vanishes, can someone please help with this.

Code:
Sub AddButton()
Dim objMenu As Object
Dim objViewMenu As Object

With Application.CommandBars("Worksheet Menu Bar").Controls
Set objMenu = .Add(Type:=msoControlButton, ID:=1183)
objMenu.Caption = "JAYEMAN"
End With

With Application.CommandBars("Worksheet Menu Bar").Controls("JAYEMAN")
objMenu.OnAction = "FORM1"

End With
End Sub

I set the script to temporary = false but that doesn't help

Code:
With Application.CommandBars("Worksheet Menu Bar").Controls
Set objMenu = .Add(Type:=msoControlButton, ID:=1183, TEMPORARY:=False)
objMenu.Caption = "JAYEMAN"
End With
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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