insert comments

kamranyd

Board Regular
Joined
Apr 24, 2018
Messages
142
Office Version
  1. 2021
Platform
  1. Windows
is there any vba code for inserting comments in a active cell and if a cell has already comments than show pop message that cell has already comments do you want to edit or no or delete comments.

is there any vba code who can do this.
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Editing post...
 
Last edited:
Upvote 0
Without any code if you right click on a cell with a comment and try to add a comment. You will see edit comment. Which tells you there is already a comment in that cell.

How are you entering comments?
Manually or part of a larger script?

If using a script show me your script.
 
Upvote 0
How about this:

Code:
Sub CkComments()
    
    Dim cmt As Comment
    Dim resp As String, newcmt As String


    For Each cmt In ActiveSheet.Comments
        If Not cmt.Shape.DrawingObject.Text = "" Then
            resp = MsgBox("Comment found in Cells(" & cmt.Shape.TopLeftCell.Cells.Row _
            & "," & cmt.Shape.TopLeftCell.Cells.Column & ")" & " it reads:" & vbNewLine & _
            vbNewLine & cmt.Shape.DrawingObject.Text & vbNewLine & vbNewLine & "Do you want to change or remove the comment", vbYesNo)
                If resp = vbYes Then
                    newcmt = InputBox("Enter New Comment or enter ""Delete"" to delete the comment")
                    cmt.Shape.DrawingObject.Text = newcmt
                        If newcmt = "Delete" Then
                            cmt.Delete
                        End If
                End If
        End If
    Next
    MsgBox "All Commnents Checked"
    
End Sub
 
Upvote 0
or try this..

Code:
[I][COLOR=#ff0000]Place this code in the SHEET module and then double click on any cell to test[/COLOR][/I]
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim OldComment As String, NewComment As String
    Cancel = True
    If Target.Count > 1 Then Exit Sub
    
    If Not Target.Comment Is Nothing Then
        OldComment = Target.Comment.Text
        Target.Comment.Delete
    End If
    NewComment = InputBox("Amend or delete comment", "", OldComment)
    If Not NewComment = vbNullString Then Target.AddComment Text:=NewComment
End Sub
 
Upvote 0
yes that i know but my sheet is protected when it is protected i cant insert comments. i want to insert comment in protected sheet.
 
Upvote 0
yes that i know but ...

Where in your request you said the sheet is protected ??? You said:

is there any vba code for inserting comments in a active cell and if a cell has already comments than show pop message that cell has already comments do you want to edit or no or delete comments.

is there any vba code who can do this.
 
Upvote 0
yes forgot that to mention sorry.
Code:
Sub InsertComment()    
    Dim ans As String, oComment, Cmnt As String
    ActiveSheet.Unprotect
    Cmnt = InputBox("Select Cell for your comments", "Your Comments")
    With ActiveCell
        If .Comment Is Nothing Then
              .NoteText Cmnt
        Else
             ans = MsgBox("Yes = Add more comments with previous comments" & Chr(10) & "No = Replace your comments with previous comments" & Chr(10) & "Cancel = Cancel for no comments", vbYesNoCancel + vbInformation, "Options")
              If ans = vbYes Then
                  oComment = .Comment.Text
                 .NoteText oComment & Chr(10) & Cmnt
              ElseIf ans = vbNo Then
                  .NoteText Cmnt
           End If
       End If
       ActiveSheet.Protect
    End With
   End Sub

i m using these codes and it is fine, but i was wondering if there are any other ways.
 
Upvote 0
Code:
Sub InsertCommentInProtectedSheet()

    Dim OldComment As String, NewComment As String, Target As Range
    Set Target = ActiveCell
    ActiveSheet.Unprotect 

    If Not Target.Comment Is Nothing Then
        OldComment = Target.Comment.Text
        Target.Comment.Delete
    End If
    NewComment = InputBox("Amend or delete comment", "", OldComment)
    If Not NewComment = vbNullString Then Target.AddComment Text:=NewComment

    ActiveSheet.Protect 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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