问题
This is giving me way more trouble than it should.
I have comments on an excel spreadsheet. I have one button. When a user clicks the button, the comments should show. When they click it again, the comments should go away. This 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 no matter what I try:
Sub showcomments()
If Comments <> "Visible" Then Application.DisplayCommentIndicator = xlCommentAndIndicator
Else: Application.DisplayCommentIndicator = xlCommentIndicatorOnly
End If
End Sub
I've tried all variations of spacing, indenting, etc. I've tried else if comments = visible; nothing seems to work for what should be such a simple task. I usually get the error "else without if" despite the fact that it's right there.
Thanks :)
回答1:
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
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/19014723/simple-vba-show-hide-excel-comments-problems