1

I have a UDF that makes a calculation based on a bunch of input variables. I am trying to make the UDF create a cell comment that contains information about those variables, but I am just getting the #VALUE! error.

This is what I am trying to do:

Function profit(income As Single, loss As Single) As Single
Dim cell As Range
cell = ActiveCell

profit = income - loss

call create_comment(cell, income, loss)

End function

Calling the sub:

Private Sub create_comment(cell As Range, income As Single, loss As Single)

cell.ClearComments
cell.Addcomment "income =" & income & "loss =" & loss

End Sub

All help appreciated!

7
  • Have a look at the limitations of an UDF. And then you can have a look at It can be done. But if you do that then you are probably on your own :-) Commented Oct 18, 2018 at 9:05
  • Thanks, I have. I am unsure whether creating a cell comment counts a formatting. If it doesn't, it should be possible. Commented Oct 18, 2018 at 9:09
  • Comments are no formatting but you can also not add a shape with a UDF. Commented Oct 18, 2018 at 9:11
  • Note that I am trying to add the comment to the same cell as the function is in. I am not trying to change the value of another cell. Thanks Commented Oct 18, 2018 at 9:13
  • I have to correct my comment from above. You cann indeed add a shape with a UDF. I did not expect that. Commented Oct 18, 2018 at 9:14

1 Answer 1

0

I changed your function like that

Function profit(income As Single, loss As Single, cell As Range) As Single
'Dim cell As Range
'cell = ActiveCell

profit = income - loss

Call create_comment(cell, income, loss)

End Function

And you enter the active cell in the call of the function like that

=profit(1000,10,H10)
Sign up to request clarification or add additional context in comments.

4 Comments

Did this work out for you? I am now getting the #NAME? error
Yes, this worked for me. Maybe you just add the code to an empty sheet or press F9 to re-calculate. I work with Office/Excel 365.
Thanks!! You are a hero!!
Glad I could help. I also learned something ;-) Please mark the question as answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.