The contents of cell A1 is =test(2) where test is the function:

Function test(ByRef x As Double) As Double
  Range("A2") = x
  test = x * x
End Function

Can you explain why this gives #VALUE! in cell A1 and nothing in cell A2? I expected A2 to contain 2 and A1 to contain 4. Without the line Range("A2") = x the function works as expected (squaring the value of a cell).

What is really confusing is if you wrap test with the subroutine calltest then it works:

Sub calltest()
  t = test(2)
  Range("A1") = t
End Sub

Function test(ByRef x As Double) As Double
  Range("A2") = x
  test = x * x
End Function

But this doesn't

Function test(ByRef x As Double) As Double
  Range("A2") = x
End Function
Edit
Report