Tony Kenny
Tony Kenny

Reputation: 1

Object Variable not set on Comments within Cells

I'm trying to determine the length of a cell's comment but I get an object variable not set when there is no comment within the Cell.

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

Upvotes: 0

Views: 201

Answers (1)

Matteo NNZ
Matteo NNZ

Reputation: 12695

This happens because in that case Range("C8").Comment is Nothing, so the code fails when trying to retrieve the Text property of Nothing.

You need a null check:

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

Upvotes: 1

Related Questions