I created a figure:

Sub gen_button() l = ActiveSheet.Range("H10:H11").Left t = ActiveSheet.Range("H10:H11").Top w = ActiveSheet.Range("H10:H11").Width h = ActiveSheet.Range("H10:H11").Height ActiveSheet.Shapes.AddShape(msoShapeRectangle, l, t, w, h).Select Selection.ShapeRange.Line.Visible = msoFalse Selection.Text = "Генерировать" Selection.Font.Color = RGB(0, 0, 0) Selection.HorizontalAlignment = 1 Selection.ShapeRange.Fill.ForeColor.RGB = RGB(0, 255, 0) End Sub 

But I also want to assign a macro call to it, how to do it?

    2 answers 2

    Use the OnAction property:

     Sub gen_button() l = ActiveSheet.Range("H10:H11").Left t = ActiveSheet.Range("H10:H11").Top w = ActiveSheet.Range("H10:H11").Width h = ActiveSheet.Range("H10:H11").Height Set shp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, l, t, w, h) shp.OnAction = "Shape_Click" shp.Select Selection.ShapeRange.Line.Visible = msoFalse Selection.Text = "Генерировать" Selection.Font.Color = RGB(0, 0, 0) Selection.HorizontalAlignment = 1 Selection.ShapeRange.Fill.ForeColor.RGB = RGB(0, 255, 0) End Sub Sub Shape_Click() MsgBox "test" End Sub 

      Along the way.

      Use the With / End With statement in macros. In such a record, the code is nicer to the eye)

      But this is not important. Parent is determined once, the code is faster

       Sub gen_button() With Range("H10:H11") l = .Left: t = .Top: w = .Width: h = .Height End With Set shp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, l, t, w, h) With shp .OnAction = "Shape_Click" .Select With Selection .ShapeRange.Line.Visible = msoFalse .Text = "Генерировать" .Font.Color = RGB(0, 0, 0) .HorizontalAlignment = 1 .ShapeRange.Fill.ForeColor.RGB = RGB(0, 255, 0) End With End With End Sub