Formatting comments with VBA

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
485
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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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