Search notes:

Excel Object Model: Button

A button object executes a macro when the user clicks on the button.
The button object is hidden (like, for example, the Global object).

Adding a button and assigning a macro

option explicit

sub main() ' {

  '
  ' Use a range to determine the coordinates where
  ' the button will be placed:
  '
    dim dest as range
    set dest = range( cells(4,2), cells(5,3) )

  '
  ' Create/add the button.
  '
    dim btn as button
    set btn  = activeSheet.buttons.add( left := dest.left, top := dest.top, width := dest.width, height := dest.height)
    btn.caption  = "Click me!"

  '
  ' Assign a macro (sub) to the button:
  '
    btn.onAction = "btnClicked"

end sub ' }

sub btnClicked() ' {

    msgBox "The button was apparently clicked"

end sub ' }
Github repository about-MS-Office-object-model, path: /Excel/Button/add.bas
See also some general notes on the onAction property of form controls

See also

The Creating a CommandButton with an OLEObject.
Form controls
Excel Object Model

Index