Excelmacros – 编辑后隐藏注释
我有这个用于在用户插入C或P时在Excel工作表上input注释,并且我需要在编辑后隐藏注释。
Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range Set KeyCells = Range("A1:S1000") If Not Application.Intersect(KeyCells, Range(Target.Address)) _ Is Nothing Then Select Case Range(Target.Address) Case "C": minhaCelula = Target.Address Range(minhaCelula).AddComment ("") Range(minhaCelula).Comment.Visible = True Case "P": minhaCelula = Target.Address Range(minhaCelula).AddComment ("") Range(minhaCelula).Comment.Visible = True End Select End If End Sub
该代码的一些问题:
-
Select Case Range(Target.Address)
没有任何意义 – 它取Target
范围,取其地址并从该地址创build一个指向原始Target
范围的范围,最后VB取该范围的默认属性,因为它没有在对象引用上下文中使用。 所以应该用Target.Value
代替整个事情。 - 后来同样的事情发生在
minhaCelula
。 -
"C"
和"P"
下的代码是相同的,应放在同一个Case
分支下。 - 在VB的
Select Case
中不使用冒号。 -
AddComment
应该没有括号 。 更好的是,您应该从AddComment
返回对添加的注释的引用中受益,所以您可以直接使用它(在这种情况下,您必须保留括号)。
所以应该重写为:
Private Sub Worksheet_Change(ByVal Target As Range) If Not Application.Intersect(Me.Range("A1:S1000"), Target) Is Nothing Then Select Case Target.Value Case "C", "P" Target.AddComment("").Visible = True End Select End If End Sub
至于这个问题,当您使用Comment.Visible
,Excel停止pipe理评论的可见性。 要离开Excel侧的pipe理,请改变注释的形状:
Private Sub Worksheet_Change(ByVal Target As Range) If Not Application.Intersect(Me.Range("A1:S1000"), Target) Is Nothing Then Select Case Target.Value Case "C", "P" With Target.AddComment("").Shape .Visible = msoTrue .Select End With End Select End If End Sub