Formatting comments with VBA

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
Good morning!

I have some code that creates a comment in the selected cell based on the username and date.

VBA Code:
Sub AddComment()

Application.ScreenUpdating = False

ActiveCell.FormulaR1C1 = "'" & Format(Now, "m/dd")
  
    Dim vCellValue As Variant
    Dim sText As String
  
    vCellValue = ActiveCell.Value
    If IsNumeric(vCellValue) Then
        vCellValue = CDbl(vCellValue)
    End If
      
    sText = Application.UserName & ":" & vbCrLf
    sText = sText & "Sent to cc on" & vbCrLf
    sText = sText & Format(Now, "M/DD/YY H:MM AM/PM")
  
    With ActiveCell
        .ClearComments
        With .AddComment
            .Text sText
            With .Shape
                .TextFrame.Characters(1, InStr(sText, ":")).Font.Bold = msoTrue
                .Width = 180
                .Height = 60

            End With
        End With
    End With
     
    Application.ScreenUpdating = True
  
End Sub

Here's some code I have that formats comments on the entire worksheet. Can I apply this functionality to the code above so that it only formats the comment in the current cell and not the whole sheet?

VBA Code:
Sub CommentFormat()
Application.ScreenUpdating = False
Dim xWs As Worksheet
Dim xComment As Comment
For Each xWs In Application.ActiveWorkbook.Worksheets
    For Each xComment In xWs.Comments
        With xComment.Shape.TextFrame.Characters.Font
            .Name = "Tahoma"
            .Size = 12
        End With
        With xComment.Shape.TextFrame
            .AutoSize = True
        End With
    Next
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
How about
VBA Code:
Sub AddComment()

Application.ScreenUpdating = False

ActiveCell.FormulaR1C1 = "'" & Format(Now, "m/dd")
  
    Dim vCellValue As Variant
    Dim sText As String
  
    vCellValue = ActiveCell.Value
    If IsNumeric(vCellValue) Then
        vCellValue = CDbl(vCellValue)
    End If
      
    sText = Application.UserName & ":" & vbCrLf
    sText = sText & "Sent to cc on" & vbCrLf
    sText = sText & Format(Now, "M/DD/YY H:MM AM/PM")
  
    With ActiveCell
        .ClearComments
        With .AddComment
            .Text sText
            With .Shape
                .TextFrame.Characters(1, InStr(sText, ":")).Font.Bold = True
                .Width = 180
                .Height = 60
                With .TextFrame.Characters.Font
                   .Name = "Tahoma"
                   .Size = 12
                End With
                With .TextFrame
                   .AutoSize = True
                End With
            End With
        End With
    End With
     
    Application.ScreenUpdating = True
  
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub AddComment()

Application.ScreenUpdating = False

ActiveCell.FormulaR1C1 = "'" & Format(Now, "m/dd")
 
    Dim vCellValue As Variant
    Dim sText As String
 
    vCellValue = ActiveCell.Value
    If IsNumeric(vCellValue) Then
        vCellValue = CDbl(vCellValue)
    End If
     
    sText = Application.UserName & ":" & vbCrLf
    sText = sText & "Sent to cc on" & vbCrLf
    sText = sText & Format(Now, "M/DD/YY H:MM AM/PM")
 
    With ActiveCell
        .ClearComments
        With .AddComment
            .Text sText
            With .Shape
                .TextFrame.Characters(1, InStr(sText, ":")).Font.Bold = True
                .Width = 180
                .Height = 60
                With .TextFrame.Characters.Font
                   .Name = "Tahoma"
                   .Size = 12
                End With
                With .TextFrame
                   .AutoSize = True
                End With
            End With
        End With
    End With
    
    Application.ScreenUpdating = True
 
End Sub

Perfect! Thanks.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,684
Messages
6,126,199
Members
449,298
Latest member
Jest

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