Name

shapes.AddLabel(Orientation, Left, Top, Width, Height)

Synopsis

Draws a text box without a border and returns the label’s Shape object.

Argument

Settings

Orientation

An msoTextOrientation constant. Can be msoTextOrientationDownward, msoTextOrientationHorizontal, msoTextOrientationHorizontalRotatedFarEast, msoTextOrientationMixed, msoTextOrientationUpward, msoTextOrientationVertical, or msoTextOrientationVerticalFarEast.

Left

The horizontal position of the shape in points.

Top

The vertical position of the shape in points.

Width

The width of the shape in points.

Height

The height of the shape in points.

Use the TextFrame property to get or set the text in the label and to set the formatting of the text. The following code draws a label on the active sheet:

Sub DrawLabel( )
    Dim s As Shape
    ' Create label (height/width will be set automatically).
    Set s = ActiveSheet.Shapes.AddLabel(msoTextOrientationHorizontal, _
      100, 100, 1, 1)
    s.TextFrame.Characters.text = "This is some label text"
End Sub

The Width and Height arguments in the preceding code are required but arbitrary because the TextFrame object’s AutoSize property is True by default. The label is automatically resized to fit the text.

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.