Adding command buttons to worksheet at run time and also define events

笑着哭i 提交于 2019-11-29 16:17:33

The easiest way to work this sort of thing out is to record a macro, then perform the action, and see what code gets recorded. In this case I recorded a macro and added a button to the sheet and got the code snippet:

Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveSheet.Buttons.Add(126.75, 39.75, 46.5, 19.5).Select
End Sub

You should be able to take it from there...

it can help, probably you will wonder also how to access it;

Sub addButton()

Dim myButton As OLEObject



Set myButton = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=0, Top:=300, Height:=20, Width:=200)

myButton.Placement = XlPlacement.xlFreeFloating

myButton.Object.Caption = "Click Me..."

myButton.Name = "DynamicButton"


End Sub

Private Sub DynamicButton_Click()



   MsgBox "Hello sheet"



End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!