Button

R

Ron de Bruin

Try this
It will run your sub mymacro

Sub test()
Dim WS As Worksheet
Dim Btn As OLEObject
Set WS = ThisWorkbook.Worksheets("Sheet1")
With WS
Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Left:=.Range("C3").Left, Top:=.Range("C3").Top, _
Width:=100, Height:=30)
End With
Btn.Object.Caption = "Click Me"
Btn.Name = "TheButton"
With ThisWorkbook.VBProject.VBComponents(WS.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", Btn.Name) + 1, _
"mymacro"
End With
End Sub

Sub mymacro()
MsgBox "Hi"
End Sub
 
T

Tom Ogilvy

with code or manually?

make the forms toolbar visible. Click on the button. Use the mouse to draw
a rectangle on the worksheet where you want it.

You should be prompted to assign a macro, select the existing macro.
 
G

Gavin Smith

Show the "Forms" toolbar. Choose the "Button" control and
draw it on your spreadsheet. Wizard will ask you what
macro to associate it with

Gavin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top