vba add shape to worksheet in Excel
Q188. In Excel, how do I add a shape using VBA?
To do it in Excel, here is the answer:
- Option Explicit
- Sub AddShapes()
- 'Add a Rectangle.
- ActiveSheet.Shapes.AddShape msoShapeRectangle, 20, 20, 50, 50
- 'Add a Rounded Rectangle.
- ActiveSheet.Shapes.AddShape msoShapeRoundedRectangle, 20, 120, 50, 50
- 'Add a 4-point star.
- ActiveSheet.Shapes.AddShape msoShape4pointStar, 20, 220, 50, 50
- End Sub
a) Line 5, 8, 11 - the 4 numbered arguments after the Shape type are as follows -
1) Position of Shape from Left Edge of the WorkSheet
2) Position of Shape from Top Edge of the WorkSheet
3) Shape Width
4) Shape Height
b) For getting supported Shape Types, consult MSDN, potential source -
Result after Macro execution: