1

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.

4
  • If you're using your function like this in VBA - myFunction(1,2) try putting call in front of it so it would be Call myFunction(1,2) - or you can remove the brackets so it'd be myFunction 1,3 Commented Jan 12, 2021 at 10:19
  • Your function should work as written (works here). What, exactly, is the value of y that you are feeding in. If it looks like a number, are there any other (non-printing) characters in the cell that would cause VBA to interpret it as text? (check by doing len(y)) Commented Jan 12, 2021 at 12:04
  • I have not defined y besides in the equation and even forgetting about y, I get the #VALUE! error even if I called my function as =myFunction(1,2) in order to make it sum the variable to get 3. The issue arises only when I add that second variable Commented Jan 12, 2021 at 12:49
  • OK. Is it possibly the case that your separator is the semicolon? If so, you would need to (on the worksheet), enter the formula as =myFunction(1;2) *By the way, to alert a commenter that you have posted a response to them, use @commenters_name in your comment) Commented Jan 12, 2021 at 17:20

1 Answer 1

1

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())
Sign up to request clarification or add additional context in comments.

2 Comments

I am defining my function as =myFunction(A1,A2) and still getting #VALUE! It even does not work if I use =myFunction(1,2)
@Dilan I do not have a Windows PC right now. Put a breakpoint on the line contains x + y. Check out the type of each parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.