Create command button with VBA

davers5

Active Member
Joined
Feb 20, 2002
Messages
255
I want to add a new workbook and then create a command button on the worksheet and assign a macro to that button. How can I do this?

Code would be something like this.

Sub AddNewBook()

workbooks.add xlWBATWorksheet
'Create button
'Assign macro

end sub

Thanks for the help!

Dave S.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Commandbutton.1", Link:=True _
, DisplayAsIcon:=False, Left:=6, Top:=91, Width:=77, Height:=33). _
Select

This code will put the button on the active sheet. You would then have to create code for the onclick event for the button on the worksheet that it was being placed on.

Not sure if you can assign a macro without putting the code on the worksheet but this should get you started.

DaK
 
Upvote 0
Thank you DaK. Your solution did not work. However, in trying to figure out the details of what was actually happening, I had a moment of clarity. All I need to do was record the darn macro and see what it came up with. After doing that and fiddling with the code I found the solution and it was suprisingly easy. Here it is:

Sub CreateButton()
ActiveSheet.Buttons.Add(199.5, 20, 81, 36).Select
Selection.Name = "New Button"
Selection.OnAction = "CheckTotals"
ActiveSheet.Shapes("New Button").Select
Selection.Characters.Text = "Check Totals"
End Sub

Dave S.
 
Upvote 0
Strange it didn't work as I recorded this macro as well about 2 months ago for a project I was doing. Oh well, at least it's solved.

DaK
 
Upvote 0
DaK,

I didn't mean that it didn't work. It created the command button, but like you said, I couldn't figure out how to do anything with it (I couldn't even delete it!). Still, it was the inspiration for my solution so thanks again.

Dave
 
Upvote 0

Forum statistics

Threads
1,216,058
Messages
6,128,538
Members
449,456
Latest member
SammMcCandless

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