0

I have a created this macro with the Record Macro function of Excel:

Sub Macro1()
    Range("J2").Select
    Range("J2").AddComment
    Range("J2").Comment.Visible = False
    Range("J2").Comment.Text Text:="qsd"
    Range("L8").Select
End Sub

When running the sub function created like this:

Sub test()
    Call Macro1
End Sub

I get this error: "Object variable or With block variable not set".

The error is on line 3 according to the debugger: enter image description here

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Does this answer your question? [Add comments to cells using VBA](https://stackoverflow.com/questions/45600673/add-comments-to-cells-using-vba) - (You need to check if one exists first) – braX Nov 22 '22 at 17:01
  • 1
    @braX - if a comment exists already tho, I think this code throws a RTE 1004. – BigBen Nov 22 '22 at 17:06
  • You don't just check to see if it's `Nothing` first? – braX Nov 22 '22 at 17:09
  • Meaning, if you run this code twice, you don't get RTE 91, but a RTE 1004... so I can't reproduce the issue. Though I don't disagree with you that one should check if a comment already exists first. I just can't repro specifically a RTE 91. – BigBen Nov 22 '22 at 17:10
  • 1
    I can get RTE 91 is if I declare a Function `Range()` with a single string-typed parameter, but which does not return a Range object. There may be other ways to get that though... Do you have a method named `Range()` somewhere in your VBA project? – Tim Williams Nov 22 '22 at 17:47

1 Answers1

1

The problem was indeed that there was already a comment.