Autosize Cell Comments/Shapes via Macro Code

  • Hi,
    Among many attempts to insert and autosize a comment using VBA manually, which errored, i recorded the following bit. When running the macro, it does format the cell - text, not the comment - text and errors in the line highlighted in red.


    I just want to add

    Code
    .AutoSize = True

    or something to that effect to the bit below. Any suggestions?

    Code
    Range("A14").Comment.Text "Ladida"


    Thank you,
    Stefan

  • Re: autosize comment errors with recorded macro


    The selection is not refering to the shape textframe.


    Try this
    [vba] With Range("A15")
    .AddComment
    With .Comment
    .Visible = False
    .Text Text:="Ladida"
    With .Shape
    With .TextFrame
    .HorizontalAlignment = xlLeft
    .VerticalAlignment = xlTop
    .ReadingOrder = xlContext
    .Orientation = msoTextOrientationHorizontal
    .AutoSize = True
    End With
    End With
    End With
    End With
    [/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: autosize comment errors with recorded macro


    Hi Andy,

    Quote

    The selection is not refering to the shape textframe.

    Thank you. - Wonder why the Macro Recorder is doing such garbage? - On the other hand, i suppose that there is not a "1-line approach" to autosizing the comment-box.
    Stefan

  • Re: Autosize Cell Comments/Shapes via Macro Code


    To just autosize you can use
    [vba]ActiveCell.Comment.Shape.TextFrame.AutoSize = True[/vba]


    The macro recorder did not exactly record garbage. It just missed the 1 line of code which would have made the final block of selection code valid. That line is the one where you select the actual comment.

    [h4]Cheers
    Andy
    [/h4]

  • Re: Autosize Cell Comments/Shapes via Macro Code


    Andy,
    Thank you for your help and explanation, much appreciated.


    Dave,
    Thank you for the heads up and for adjusting the thread title.


    Stefan

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!