-1

I have follow code:

Public Function myfunc()
    ActiveSheet.Range("A1").Interior.Color = vbGreen
    myfunc = ""
End Function

Why when I type =myfunc() in cell B1 it is return #VALUE! error and don't fill A1 cell?

enter image description here

Also, why it make cell A1 green, when i press function icon?

enter image description here

Your Mammy
  • 334
  • 2
  • 10

1 Answers1

1

A UDF in a cell can only return a value to the cell in which it resides. It can't effect changes to other cells. You need a SUB not a function.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
  • so way why it is work, when i press function icon? – Your Mammy Sep 08 '21 at 21:26
  • @WaleronTrot - seems like that function icon runs your function outside of the "worksheet calculation" sandbox. It would also work if you called the function from a Sub instead. See related: https://stackoverflow.com/questions/20866484/can-i-disable-a-vba-udf-calculation-when-the-insert-function-function-arguments – Tim Williams Sep 08 '21 at 21:31