对象variables未设置在单元格内的注释

我试图确定一个单元格的评论的长度,但我得到一个对象variables没有设置单元格内没有评论。

Len(Range("C8").Comment.Text)

发生这种情况是因为在这种情况下, Range("C8").CommentNothing ,因此在尝试检索NothingText属性时代码失败。

你需要一个空检查:

 If Not Range("C8").Comment Is Nothing Then length = Len(Range("C8").Comment.Text) End If