Dylan
Dylan

Reputation: 11

VBA defined function not accepting more than one variable

My VBA Function is not accepting two variables for a basic function. It keeps giving a #VALUE! error.

Function myFunction(x As Integer, y As Integer) As Integer
    result = x + y
    myFunction = result
End Function

However, it works fine if I simply remove the y variable. For example, this works perfectly:

Function myFunction(x As Integer) As Integer

    result = x + x
    myFunction = result

End Function

I am using Windows 10 Pro on a Lenovo Yoga.

Upvotes: 1

Views: 258

Answers (1)

Tarik
Tarik

Reputation: 11209

If you are using invoking your function with an Excel range with a formula such as =myFunction(A1:A2) an array will be sent as the first parameter and the second parameter will be missing and cause an error with the ultimate result you are getting.

Note: =myFunction(A1, A2) should work.

If you indeed are using a range, define myFunction as follows:

Function myFunction (x())

Upvotes: 1

Related Questions