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

这给了我更多的麻烦。

我有一个Excel电子表格的意见。 我有一个button。 当用户点击button时,应该显示注释。 当他们再次点击时,评论应该消失。 这是我试图使用的代码 – 他们都独立工作,但是当我把一个If Then Else的声明,我得到错误,无论我尝试:

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

我已经尝试所有的间距,缩进等变化我已经尝试其他如果注释=可见; 似乎没有什么工作,这应该是一个这么简单的任务。 我通常会得到错误“其他没有,如果”,尽pipe它在那里。

谢谢 :)

尝试这个:

  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 

本来想在Excel中做这个。 如果我没有弄错,这对于你想要的并且不需要循环或者额外的全局variables是完全正确的。

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

这个方法使用了一个全局variables,并且不必遍历所有的注释。

 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