vba to reference a particular line of text in a cell's comment box

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
679
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have the code below which works fine, however how would I adjust or add to the code to change the text to bold on just the first line in the comment box, rather than all lines?
Ultimately I would want to be able to specify the line number, eg. line 1, line 5 etc..
Any help much appreciated.
Thanks.
VBA Code:
Sheet12.Select
    Range("D9").Comment.Text Text:="Feedback:" & Chr(10) & Range("F4") & Chr(10) & Range("F5")
    Range("D9").Comment.Shape.TextFrame.Characters.Font.Size = 10
    Range("D9").Comment.Shape.TextFrame.Characters.Font.Bold = True
    Range("D9").Comment.Shape.TextFrame.AutoSize = True
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You need to look at the correct characters:
Rich (BB code):
Range("D9").Comment.Shape.TextFrame.Characters(1, 9).Font.Bold = True

1 is the start, 9 is the length - maybe use INSTR to find the first Chr(10) character.
 
Upvote 0
Hi,
Thanks for responding to this.
I understand what you are saying but the number pf characters in the Ranges F4 and F5 (for example) will be variable.
 
Upvote 0
This might work better, although it's not perfect.
Pass the EditComment procedure a single range, the comment and optionally which line you want making bold.

Rich (BB code):
Sub Test()
    EditComment Sheet1.Range("A9"), "Feedback:" & Chr(10) & Range("F4") & Chr(10) & Range("F5"), 2
End Sub
Public Sub EditComment(Target As Range, CommentText As String, Optional BoldLine As Long)
    Dim NewLines As Long
    Dim CommentLines As Long
    Dim Start As Long, Finish As Long
    Dim TextLines As Variant
    Dim y As Long
    With Target
        If .Cells.Count = 1 Then
            NewLines = Len(CommentText) - Len(Replace(CommentText, Chr(10), ""))
            
            'Add a comment if it doesn't exist, otherwise add text to existing comment.
            If .Comment Is Nothing Then
                CommentLines = 0
                .AddComment CommentText
            Else
                CommentLines = Len(.Comment.Text) - Len(Replace(.Comment.Text, Chr(10), ""))
                .Comment.Text Text:=.Comment.Text & Chr(10) & CommentText
            End If
            
            If BoldLine > 0 Then
                'Find the specified line number and make it bold.
                'Problem with this is that it will remove any existing formatting in the comment.
                TextLines = Split(.Comment.Text, Chr(10))
                For y = 0 To CommentLines + BoldLine - 1
                    Start = Start + Len(TextLines(y)) + 1
                Next y
                Finish = Start + Len(TextLines(y))
                .Comment.Shape.TextFrame.Characters(Start + 1, Finish - Start).Font.Bold = True
            End If
        End If
    End With
End Sub
 
Upvote 0
Hi,

Thanks for coming back and for your time.

A couple of things:

1) I wanted the first line 'Feedback:' to be in bold so I changed the last number in the Sub Test () Edit Comment line from '2' to '1' but it made the second line of text from Range("F4") bold instead. I then changed it to '0' and then no lines were bold?
2) Also I would always want the comment to override any existing comment, rather than add to it, can this be achieved?
3) Lastly, the number of cells with text being put into the comment box may vary, so I may also need to add, for example; & Chr(10) & Range("F6") & Chr(10) & Range("F7") - would the code be able to handle these additions?

Thanks again for your help.
 
Upvote 0
Sorry, didn't test it properly.

Having a new comment each time makes life easier. Code below will delete an existing comment and add a new. It will then turn the first line bold.
If your first line is always 'Feedback:' then my first answer will work - it just turns the first 9 characters bold, anything after that isn't - the length of text in F4 & F5 doesn't matter.

Rich (BB code):
Sub Test()
    AddComment Sheet2.Range("A9"), "Extra Feedback:" & Chr(10) & Range("F4") & Chr(10) & Range("F5")
End Sub
Public Sub AddComment(Target As Range, CommentText As String)
    With Target
        If .Cells.Count = 1 Then
            If Not .Comment Is Nothing Then
                .Comment.Delete
            End If
            .AddComment CommentText
            .Comment.Shape.TextFrame.Characters(1, InStr(CommentText, Chr(10))).Font.Bold = True
        End If
    End With
End Sub
 
Upvote 0
That works brilliantly, thanks very much :)

Sorry to add onto the original requirement, but I have one last question if I may:

If I additionally wanted to make the second line in the comment box have a red font could I just make a change to this line in some way to refer to the second line (the text from Range F4)...

.Comment.Shape.TextFrame.Characters(1, InStr(CommentText, Chr(10))).Font.Bold = True

Many thanks.
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,090
Members
448,944
Latest member
sharmarick

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