我要重写这个请求。我发现我可以更清楚。
在电子表格中,我突出显示要在其中插入标准化注释的单元格。我使用键盘快捷键来激活AddServiceNote
并且代码插入具有标准化格式和文本的注释。
一切都有效,但不可重复。我无法选择另一个单元格并使用相同的键盘快捷键添加另一个评论。
我可以使用另一个键盘快捷键来激活FormatNotes
所有评论的格式都在其中。神秘的是,这重新激活了我插入另一个标准化评论的能力AddServiceNote
.
我错过了一些明显的东西吗?为什么我不能反复插入评论?这有关系吗Set Note = ActiveCell.Comment
?
Public Note As Comment
Public Sub AddServiceNote()
If Note Is Nothing Then
ActiveCell.AddComment
Set Note = ActiveCell.Comment
Note.Text "Function: "
OrganizeElements
End If
End Sub
Public Sub FormatNotes()
For Each Note In ActiveSheet.Comments
OrganizeElements
Next
End Sub
Public Sub OrganizeElements()
Note.Shape.TextFrame.AutoSize = True
'and a long list of other attributes
End Sub
发生的事情是这样的:
- 您有一个模块范围变量,
Note
。它的价值依然存在。
- 第一次跑步时
AddServiceNote
, Note Is Nothing
是 TRUE,所以If Then
代码运行
- In that
If
code, Note
is Set
对某事
- 下次你跑步的时候
AddServiceNote
, Note Is Nothing
是 FALSE,所以If Then
代码确实not run
- Running
FormatNotes
执行一个For
循环、设置Note
在每次迭代中
- 最后一次迭代之后
For
loop, Note
留下为Nothing
(这是一个副作用For
)
- 所以,跑完
FormatNotes
, AddServiceNote
将再次工作(一次)
修复方法很简单
- 移动
Dim Note As ...
inside AddServiceNote
(发布的代码中没有任何内容需要Note
属于模块范围)
- Change
OrganizeElements
的签名
Public Sub OrganizeElements(Note As Comment)
- 更改呼叫的线路
OrganizeElements
通过Note
作为参数:
OrganizeElements Note
Public Sub AddServiceNote()
Dim Note As Comment
If ActiveCell.Comment Is Nothing Then
Set Note = ActiveCell.AddComment
Note.Text Text:="Function: "
OrganizeElements Note
End If
End Sub
Public Sub FormatNotes()
Dim Note As Comment
For Each Note In ActiveSheet.Comments
OrganizeElements Note
Next
End Sub
Public Sub OrganizeElements(Note As Comment)
Note.Shape.TextFrame.AutoSize = True
'and a long list of other attributes
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)