简单的 VBA 显示/隐藏 Excel 注释问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19014723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:30:31  来源:igfitidea点击:

Simple VBA Show/Hide Excel Comments problems

excelvbaexcel-vbacomments

提问by Adam

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:

我对 Excel 电子表格有意见。我有一个按钮。当用户单击按钮时,应显示评论。当他们再次单击它时,评论应该消失。这是我尝试使用的代码 - 它们都独立工作,但是当我放入 If Then Else 语句时,无论我尝试什么都会出错:

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.

我已经尝试了所有的间距、缩进等变化。如果评论=可见,我已经尝试过其他的;对于应该如此简单的任务,似乎没有任何作用。尽管事实上它就在那里,但我通常会收到错误“else without if”。

Thanks :)

谢谢 :)

回答by Jair Batista

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

回答by ProlificSwan

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...

我自己想在 Excel 中执行此操作。如果我没记错的话,这非常适合您想要的东西,并且不需要循环或额外的全局变量......

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

回答by Portland Runner

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