Error in code from ChatGTP

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,151
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
VBA Code:
Sub FormatComment(TheCell)
    Dim cell As Range
    Dim commentShape As Shape
    Dim desiredWidth As Single

    ' Set your desired comment width (in points)
    desiredWidth = 600

    ' Specify the cell containing the comment
    Set cell = ActiveSheet.Range(TheCell)

    ' Check if the cell has a comment
    If Not cell.Comment Is Nothing Then
        ' Access the comment's shape
        Set commentShape = cell.Comment.Shape

        ' Set the comment's width
        commentShape.Width = desiredWidth

        ' Ensure AutoSize is disabled to allow word wrap
        commentShape.TextFrame.AutoSize = False

        ' Optional: Adjust the height to fit the text
        commentShape.Height = commentShape.TextFrame.TextRange.BoundHeight + 10
    Else
        MsgBox "The specified cell does not contain a comment."
    End If
End Sub

Wordrap and width are good but the line commentShape.Height = commentShape.TextFrame.TextRange.BoundHeight + 10
Causes error
Run-time error '438':
Object doesn't support this property or method

Using Excel 2016. Height is critical, can i resolve this anyhow?
 
Is the "BoundHeight" property is available in Excel?
I am not at PC then I can not check, but you can try.
 
Upvote 0
I assumed so as desciption starts with In Excel VBA, you can....
But .TextRange.BoundHeight doesn't appear in the dropdown list.
Maybe newer versions only ? Can I get height some other way?
In order to have Wordwrap autosize must be false. So the height returned is the actual height, not the height needed to see all the text.
 
Upvote 0
just...
VBA Code:
commentShape.Height = commentShape.Height + 10
 
Last edited:
Upvote 0
and this is why we don't allow answers created with AI... ;) That property exists in PowerPoint, not Excel.
 
Upvote 0
> commentShape.Height = commentShape.Height + 10
Although that fixes the error, it doesn't help. You need Autosize false to wordwarp and then height returns a meaningless value.
You can add 10 to it but it still truncates the text.
I found ChatGPT quite interesting and useful until this. It also said
determining the number of rows it expands to is not straightforward. However, here are some approaches:
Measuring the text length.
Checking the font size and control width.
Estimating how many lines the text will wrap into.

A bit hit and miss and dependent on font size, proportional spacing etc. Has anyone succeeded with that?
I'll PowerPoint. It uses VBA. Maybe see how accurate TextFrame.TextRange.BoundHeight is and use it's value in Excel.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top