Hey Rob .. So I had some free time at home due to coronavirus curfew so it was a good opportunity to learn about Excel's new "comment" in VBA. I have revised the code significantly to accommodate for all comment & notes in all sheets of the workbook instead of only one sheet. I have also attached the sample file I was working on for your reference
VBA code for hiding all comments/notes
Sub HideAllComments()
Dim Ar1 As Variant, Cmt As Comment, CntCmt As Long
Dim Ar2 As Variant, CmtThrd As CommentThreaded, CntCmtThrd As Long
Dim Cnt As Long, Ws As Worksheet, Flg As Boolean, lRow As Long
For Each Ws In ThisWorkbook.Worksheets
If Ws.Name = "HiddenComments" Then
Ws.UsedRange.ClearContents
Flg = True
Exit For
End If
Next Ws
If Flg = False Then Sheets.Add.Name = "HiddenComments"
For Each Ws In ThisWorkbook.Worksheets
If Ws.Name <> "HiddenComments" Then
If Ws.Comments.Count > 0 Then
Cnt = 0
CntCmt = CntCmt + Ws.Comments.Count
ReDim Ar1(1 To Ws.Comments.Count, 1 To 4)
For Each Cmt In Ws.Comments
Cnt = Cnt + 1
Ar1(Cnt, 1) = Ws.Name
Ar1(Cnt, 2) = "Note"
Ar1(Cnt, 3) = Cmt.Parent.Address(0, 0)
Ar1(Cnt, 4) = Cmt.Text
Cmt.Delete
Next
lRow = Sheets("HiddenComments").Range("A" & Rows.Count).End(xlUp).Row + 1
With Sheets("HiddenComments")
.Range("A1").Resize(, UBound(Ar1, 2)) = Array("Sheet CodeName", "Type", "Comment Address", "Comment Text")
.Range("A" & lRow).Resize(UBound(Ar1), UBound(Ar1, 2)) = Ar1
End With
End If
If Ws.CommentsThreaded.Count > 0 Then
Cnt = 0
CntCmtThrd = CntCmtThrd + Ws.CommentsThreaded.Count
ReDim Ar2(1 To Ws.CommentsThreaded.Count, 1 To 4)
For Each CmtThrd In Ws.CommentsThreaded
Cnt = Cnt + 1
Ar2(Cnt, 1) = Ws.Name
Ar2(Cnt, 2) = "Comment"
Ar2(Cnt, 3) = CmtThrd.Parent.Address(0, 0)
Ar2(Cnt, 4) = CmtThrd.Text
If CmtThrd.Replies.Count > 0 Then
For y = 1 To CmtThrd.Replies.Count
Ar2(Cnt, 4) = Ar2(Cnt, 4) & "|" & CmtThrd.Replies(y).Text
Next y
End If
CmtThrd.Delete
Next
lRow = Sheets("HiddenComments").Range("A" & Rows.Count).End(xlUp).Row + 1
With Sheets("HiddenComments")
.Range("A1").Resize(, UBound(Ar2, 2)) = Array("Sheet CodeName", "Type", "Comment Address", "Comment Text")
.Range("A" & lRow).Resize(UBound(Ar2), UBound(Ar2, 2)) = Ar2
End With
End If
End If
Next Ws
Sheets("HiddenComments").Visible = xlSheetVeryHidden 'xlSheetVisible
lRow = Sheets("HiddenComments").Range("A" & Rows.Count).End(xlUp).Row
MsgBox "All Comments & Notes in this file are hidden successfully" & vbNewLine & _
CntCmtThrd & " Comments + " & CntCmt & " Notes : Total = " & lRow - 1, vbInformation
End Sub
VBA code to show back all comments/notes
Sub ShowAllComments()
Dim Pass As String: Pass = "Nankw83" '<--- change the password to whatever suits you
If Application.InputBox("Please enter password to un-hide all comments and notes") <> Pass Then
MsgBox "Incorrect Password !", vbCritical
Exit Sub
End If
Dim Arr As Variant, Ws As Worksheet
With CreateObject("scripting.dictionary")
For Each Ws In ThisWorkbook.Sheets
If Not .exists(Ws.Name) Then .Add Ws.Name, Nothing
Next
If Not .exists("HiddenComments") Then
MsgBox "No Comments or Notes are saved to be retrieved", vbExclamation
Exit Sub
End If
End With
Arr = Sheets("HiddenComments").Range("A1").CurrentRegion
For x = 2 To UBound(Arr)
With Sheets(Arr(x, 1)).Range(Arr(x, 3))
If Arr(x, 2) = "Note" Then
If .Comment Is Nothing Then
.AddComment
.Comment.Text Arr(x, 4)
Else
.Comment.Text Arr(x, 4)
End If
ElseIf Arr(x, 2) = "Comment" Then
If InStr(Arr(x, 4), "|") = 0 Then
.AddCommentThreaded (Arr(x, 4))
Else
.AddCommentThreaded Split((Arr(x, 4)), "|")(0)
For y = 1 To UBound(Split(Arr(x, 4), "|"))
.CommentThreaded.AddReply (Split(Arr(x, 4), "|")(y))
Next y
End If
End If
End With
Next x
MsgBox "All " & UBound(Arr) - 1 & " Comments & Notes are retrieved successfully", vbInformation
End Sub
Bookmarks