How can I create, add, edit and remove a comment box using coding, when the sheet is in protected mode.
Presently, in protected mode the insert comment option is greyed out not allowing me to add comments when the sheet is protected. The spreadsheets need to stay in protected mode (with only a few specific cells unlocked) as I will not be the only person using it. The 'Edit Object' boxes also need to remain unchecked when the sheets are protected.
Can someone provide me with the correct coding as to how to do this please, and/or also specify why the below codes do not work?
I'm using Excel 2016.
Thank you.
INSERT COMMENT CODE:
Public Sub InsertComment()
Dim sPassword As String
Dim MyComment As String
sPassword = "TestPassword"
Set commentCell = ActiveCell
MyComment = InputBox("Enter your comments", "Comments")
ActiveSheet.Unprotect Password:=sPassword
Range(commentCell.Address).AddComment
Range(commentCell.Address).Comment.Text Text:=MyComment
ActiveSheet.Protect Password:=sPassword
End Sub
EDIT, ADD, DELETE COMMENT CODE:
Option Explicit:
Sub myAdd_myEdit_myDelete_Comment()
Dim myAdd, myEdit, myAE
Dim MyComment As String
Dim commentCell As Range
Dim myCheck
myAE = InputBox("If adding comment enter ""Add""" _
& vbCr & vbCr & _
"If editing comment enter ""Edit""" _
& vbCr & vbCr & _
"If Deleting comment enter ""Delete""", "Comments")
If myAE = "Add" Then
ActiveSheet.Unprotect Password:="TestPassword"
Set commentCell = ActiveCell
MyComment = InputBox("Enter your Comment text...", "Comments")
If Not ActiveCell.Comment Is Nothing Then Exit Sub
Range(commentCell.Address).AddComment
Range(commentCell.Address).Comment.Text Text:=MyComment
ElseIf myAE = "Edit" Then
MyComment = InputBox("Enter your Comment NEW text...", "Comments")
ActiveCell.Comment.Text Text:=MyComment
ElseIf myAE = "Delete" Then
myCheck = MsgBox("Do you want to DELETE this Comment?", vbYesNo)
If myCheck = vbNo Then
Exit Sub
Else
ActiveCell.Comment.Delete
End If
ActiveSheet.Protect Password:="TestPassword"
End If
End Sub
REMOVE COMMENT CODE:
Sub Comment_Be_Gone()
Dim rng As Range
Set rng = ActiveCell
If Not (rng.Comment Is Nothing) Then rng.Comment.Delete
End Sub
Sub Remove_All_Comments_From_Worksheet()
Cells.ClearComments
End Sub
Bookmarks