0

I have comments on an Excel spreadsheet.

I have one button to control whether the comments are displayed or hidden. When a user clicks the button, the comments should show. When the user clicks it again, the comments should go away.

Below is the code I'm trying to use - they both work independently, but when I put in an If Then Else statement, I get errors:

Sub showcomments()

If Comments <> "Visible" Then Application.DisplayCommentIndicator = xlCommentAndIndicator
Else: Application.DisplayCommentIndicator = xlCommentIndicatorOnly
End If
    
End Sub

I tried else if comments = visible.

I usually get the error "else without if".

1
  • It's for a dashboard I created so I'm trying to make it as simple and clean as possible. Commented Sep 25, 2013 at 20:53

4 Answers 4

4

Was wanting to do this in Excel myself. If I'm not mistaken, this works perfectly fine for what you want and requires no looping or extra global variables...

Sub showcomments()
  If Application.DisplayCommentIndicator = xlCommentIndicatorOnly Then
    Application.DisplayCommentIndicator = xlCommentAndIndicator
  Else
    Application.DisplayCommentIndicator = xlCommentIndicatorOnly
  End If
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

..this works perfect for me on Win10/MS Office 2010! Thx!
3

Try this:

   Sub showcomments()
   Comments = 1
   For Each MyComments In ActiveSheet.Comments
       If MyComments.Visible = True Then
           Comments = 0
       End If
   Next
   If Comments = 1 Then
       Application.DisplayCommentIndicator = xlCommentAndIndicator
   Else
       Application.DisplayCommentIndicator = xlCommentIndicatorOnly
   End If
End Sub

Comments

0

This method uses a global variable and doesn't have to loop through all your comments.

Public comments As Integer

Sub showcomments()
 If comments = 1 Then
   Application.DisplayCommentIndicator = xlCommentAndIndicator
   comments = 0
  Else
    Application.DisplayCommentIndicator = xlCommentIndicatorOnly
    comments = 1
  End If
End Sub

1 Comment

This one works too, thanks! Gonna try and compare the two that worked with mine and figure out what went wrong.
0

I used another approach which I find easier:

Sub Show_Hide_Comments(visible As Boolean)
  Dim cmnt As Comment
   For Each cmnt In ActiveSheet.comments
       cmnt.Shape.Visible = visible
   Next
End Sub

This finds all comments and hides their shape. You can even do this individually for certain comments if you wish.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.