Linh
Linh

Reputation: 203

Simple User defined functions from VBA dont work in Excel 2010

I am trying to create some User Defined Functions but they all result in #Value! Error.

My Macro has been enabled. I even added the Application.Volatile (as suggested on some posts). Nothing works.

These are some of my statements, they all result in the same error. 1)

Function Excer1(x)
    Application.Volatile
    Excer1(x) = x ^ 2 - 3
End Function

2)

Function Excer2(x)
    Application.Volatile
    Excer2(x) = Sqr(2 * x ^ 2) + 2 * x
End Function

3)

Function Excer3a(x1, x2)
    Excer3a(x1, x2) = Log(x2 / x1)
End Function

Upvotes: 1

Views: 79

Answers (3)

Linh
Linh

Reputation: 203

Solved. I should have not included the variable (in this case "x") when defining the functions:

Corrected versions:

Function Excer1(x)
Application.Volatile
Excer1 = x ^ 2 - 3
End Function

Function Excer2(x)
Application.Volatile
Excer2 = Sqr(2 * x ^ 2) + 2 * x
End Function

Function Excer3a(x1, x2)
Excer3a = Log(x2 / x1)
End Function

Upvotes: 0

Gary's Student
Gary's Student

Reputation: 96753

You were very close:

Function Excer1(x)
    Application.Volatile
    Excer1 = x ^ 2 - 3
End Function
'2)

Function Excer2(x)
    Application.Volatile
    Excer2 = Sqr(2 * x ^ 2) + 2 * x
End Function
'3)

Function Excer3a(x1, x2)
    Excer3a = Log(x2 / x1)
End Function

Upvotes: 0

Rory
Rory

Reputation: 34045

Your return lines are incorrect - the parentheses and arguments should not be included:

Function Excer1(x)
    Application.Volatile
    Excer1 = x ^ 2 - 3
End Function

Function Excer2(x)
    Application.Volatile
    Excer2 = Sqr(2 * x ^ 2) + 2 * x
End Function

Function Excer3a(x1, x2)
    Excer3a = Log(x2 / x1)
End Function

Upvotes: 2

Related Questions