Tuesday, April 5, 2011

Add "comments" on your reports as additional information

On some occasions it is necessary to have detailed information of a specific data. One option is insert a comment in a cell with the information we want, and it can be done automatically. On this “comment” we can add additional information that is not available on the report “as a first view”.


Code sample:

'Add comment
Private Sub AddMyComment()
    Dim MyRange As Range
    Set MyRange = ActiveSheet.Cells(1, 1)
        If MyRange.Comment Is Nothing Then
           MyRange.AddComment
           MyRange.Comment.Text "Please test adding a comment with VBA" & vbNewLine _
                                                & "You can add"
        End If
End Sub 

'Remove comment
Private Sub cmdRemoveComment()

Dim MyRange As Range
    Set MyRange = ActiveSheet.Cells(1, 1)
        If Not (MyRange.Comment Is Nothing) Then
                MyRange.Comment.Delete
        End If
End Sub

Or can be used like this: 

'Sheet1.Range("A1").AddComment ("Please test adding a comment with VBA")
'Sheet1.Range("A1").Comment.Delete

No comments:

Post a Comment