0

I am having a bit of trouble with the return function on VBA and was hoping someone would be able to help me troubleshoot the issue.

Here is the function:

Function Hypotenuse(side1 As Double, side2 As Double) As Double
Return Math.Sqrt((side1 ^ 2) + (side2 ^ 2))
End Function

Summary of the Issue:

  • The return function is highlighted in 'red' and I am unable to run any sub-routine that calls the function (see attached picture)
  • Pretty sure the code is code is correct, but i simply do not understand why I keep getting an error

Any idea why this may be happening?

Any help will be much appreciated :)

enter image description here

braX
  • 11,506
  • 5
  • 20
  • 33
Ibra22
  • 25
  • 6
  • You need to assign `Hypotenuse` to a `Double` Variable. – Damian May 30 '20 at 17:06
  • 1
    Does this answer your question? [How to return a result from a VBA function](https://stackoverflow.com/questions/2781689/how-to-return-a-result-from-a-vba-function) – FunThomas May 30 '20 at 17:18

1 Answers1

1

Use Sqr function .. and no Return keyword in VBA

Sub Test()
    Debug.Print Hypotenuse(5, 3)
End Sub

Function Hypotenuse(side1 As Double, side2 As Double) As Double
    Hypotenuse = Sqr((side1 ^ 2) + (side2 ^ 2))
End Function
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95