Len
Len

Reputation: 11

How to use the value of a variable to create a cell comment in Excel

I am struggling to find the syntax of how to add a comment using the value of a variable, or if it's even supported through VBA.

For example, it's easy enough to know that:

Range("YourRange").AddComment "Your Text"

...will add a comment to a range of cells with whatever "Your text" is. What I am trying to figure out is how to use a variable's value instead of "Your Text". I have tried various combinations using an &, and have yet to find any syntax online.

Receiving Error:
VBA Runtime Error 1004: Application-defined or Object-defined error

If sinput = 1 Then
    Num = InputBox("How much did you spend?", "July - " & gasString)
    Range("F11").Value = Range("F11").Value + Num
    Range("F11").AddComment Num
End If

Upvotes: 1

Views: 1378

Answers (1)

Vityata
Vityata

Reputation: 43565

Minimal example with a variable:

Sub TesetMe()

    Dim text As String
    text = "someText"
    Range("B2").AddComment text

End Sub

Upvotes: 4

Related Questions