Delete all comments which are blank or contains User Name

leonlai

Board Regular
Joined
Mar 22, 2019
Messages
77
Hi,

I am writing a macro that will delete all comments (on the worksheet) that are empty.
By "Empty" , I mean either of 2 things:

(1) The comment box is absolutely empty: nothing in it.
The user can create such a comment by selecting a cell and clicking a macro button.
The macro creates a comment without User Name.
I created the macro to save space because there may be many comments on the sheet.

(2) But if the user creates a comment using Excel's menu (Insert Comment), and forgets typing anything else, I also
consider it to be an empty comment.

The comment will contain (in bold), say MYCOMPANY:
But if the comment contains something else, say MYCOMPANY: abcdefgh, it is not empty.I tried the code below, but it deletes only those comments that are completely empty.
It does not delete those comments that contain ONLY the User Name.

It does not work even if I hard-code the User Name.


Code:
Sub DeleteEmptyComments()
  Dim C As Comment
  Dim User As String
   
  User = Application.UserName & ":"
  
For Each C In Worksheets("Sheet1").Comments
    If C.Text = User Or C.Text = "" Then C.Delete
    
  Next
End Sub

How can we modify the Sub to make it do what I want?

Thanks
Leon
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this - my Excel adds a colon after the username so this works for me
And it makes no difference who the user is

Code:
  If InStr(c.Text, ":") = Len(c.Text) Or c.Text = "" Then c.Delete

Compares position of first colon to length of comment text
 
Last edited:
Upvote 0
OR

If above does not help and getting the user name is required (I have added the colon because I need it)

you could try comparing comment text with one of these:
Code:
msgbox Application.UserName & ":"
MsgBox ActiveWorkbook.BuiltinDocumentProperties("Author").Value & ":"

But there may be more than one user adding comments - so may not be overly useful!
 
Last edited:
Upvote 0
How about this. There is an extra character automatically added if the the user just clicks "Insert Comment" and leaves the cell comment blank.

Code:
Sub DeleteEmptyComments()
    Dim C As Comment
    Dim User As String
   
    User = Application.UserName & ":"
  
    For Each C In Worksheets("Sheet01").Comments
        If C.Text = User Or C.Text = "" Or Asc(Mid(C.Text, Len(C.Text), 1)) = 10 Then C.Delete
    Next
End Sub
 
Upvote 0
The LF is added as igold states in a blank comment but they may also be found in badly edited comments

Code:
Sub DeleteEmptyComments()
  Dim C As Comment
  Dim User As String
   
  User = Application.UserName & ":"
   
 For Each C In Worksheets("Home Screen").Comments
 
    If Replace(C.Text, vbLf, "") = User Or Replace(C.Text, vbLf, "") = "" Then C.Delete
    
  Next
End Sub
 
Last edited:
Upvote 0
@dave

I agree, but it would have to be a pretty bad hack job, I don't even know that there is a single keyboard stroke that adds a LF. That coupled with the fact that it would have to appear at the very end of the comment...
 
Upvote 0
Hi, Yongle

Your code is logically correct, but there is a problem with Excel (not your code).

My User Name is:
AAA BBB ABCDEFGHIJKL
---
3+1+3 +1+ 12 = 20

If we add the colon ":" in the Comment, this gives a length of 21 (including the blanks).

I added this MsgBox to find out the relevant values:
MsgBox ("Position of colon = " & InStr(C.Text, ":") & " Length of text = " & Len(C.Text))

If I create Comments using Insert Comments, this is what the MsgBox gives:

Position of colon = 21 (correct)
Length of Text = 22 (incorrect: it's always 1 more than the correct value)

If I incorporate this correction in your formula, it works fine!
But it's not reliable, because if somebody types blanks in the comments, the formula fails.

I prefer your next formula, which works foe me.

Leon
 
Upvote 0
How about... to deal with extra spaces to right
Code:
 If InStr(RTrim(c.Text), ":") = Len(RTrim(c.Text)) Or c.Text = "" Then c.Delete
 
Last edited:
Upvote 0
Yongle,

How about... to deal with extra spaces to right
If InStr(RTrim(c.Text), ":") = Len(RTrim(c.Text)) Or c.Text = "" Then c.Delete


That's a tricky thing that's giving me headache.

Logically, it is correct. In practice, it does not give the correct answer.

I put the following in my code to see what happens (after putting your above code).

Code:
Position = InStr(RTrim(C.Text), ":")
Length = Len(RTrim(C.Text))


MsgBox ("position = " & Position & " Length = " & Length)

Message Box stubbornly displays:

Position = 21 Length = 22

Length is overstated by 1 !????

Can't understand what's wrong!

I even tried:
Code:
Length = Len(LTrim(RTrim(C.Text)))

Still gives 22.

No idea where the extra len comes from.
If you copy the comment to an Excel cell and apply the LEN formula, it gives 21 (correct).
But if you read directly from the comment, it gives 22 (bad)!

Leon
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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