繁体   English   中英

Excel VBA 读取单元格注释

[英]Excel VBA read cell comment

我正在编写代码来检查 Excel 单元格是否包含特定的线程注释,

我不明白为什么第一个代码给我错误

“对象变量或未设置块变量”

Sub test()
    Dim R As Range
    Set R = Range("E10:E205")

    For Each R In R.Cells
        If R.CommentThreaded.Text = "3" Then
                MsgBox (R.Address)
        End If
    Next R
End Sub

虽然此代码工作正常:

Sub test()
    Dim R As Range
    Set R = Range("E10:E205")

    For Each R In R.Cells
        If Not R.CommentThreaded Is Nothing Then
            If R.CommentThreaded.Text = "3" Then
                MsgBox (R.Address)
            End If
        End If
    Next R
End Sub

如果单元格R没有CommentThreaded对象。 那么当然一个不存在的R.CommentThreaded不能有一个.Text属性。

这就是为什么你首先需要检查R.CommentThreaded包含任何评论对象

If Not R.CommentThreaded Is Nothing Then

在您可以使用其.Text属性之前。


题外话:

MsgBox (R.Address)您应该删除括号MsgBox R.Address除非您真的想从将参数提交为ByRef切换到将其提交为ByVal

Answer = MsgBox(R.Address)   'parentheses needed because = is used
Call MsgBox(R.Address)       'parentheses needed because 'Call' is used
MsgBox R.Address             'don't use perenthesis without =

但这里的括号做了一些完全不同的事情(注意额外的空间!):

MsgBox (R.Address)            'parenthesis here force ByVal!
      ^
      | Extra space!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM