简体   繁体   中英

Why does my user defined excel function in Excel DNA return random number if I call the function without parentheses?

I am making a Excel DNA excel plugin. I have created some user defined function using 'ExcelFunctionAttribute'. Now when i call this function without parentheses it returns a random number.

Suppose my function is:

[ExcelFunction(Description= "Hello Function", IsVolatile = true)]
public static string Hello(string name)
{
    return "Hello "+name;
}

I have 3 scenario:

  1. If I call the function in excel as =Hello("XYZ") it return Hello XYZ.

  2. If I call the function in excel as =Hello() it gives error.

  3. If I call the function in excel as =Hello it gives a random number like -17453245.

In my this scenario I have not given parentheses. So could anyone please tell me Why this happen? How can I fix this issue?

Thanks in advance.

The number you see when calling the function with no parentheses is not a random number, but is the "Registration ID" for the user-defined function. It is the internal ID that Excel assigns to the internal name under which the UDF is registered.

When the formula only has the UDF name with no parentheses, the add-in is never called by Excel , so there is no way for the add-in to actually return something else like #NAME?.

In some cases one can even build more powerful functions using this 'feature', where you take the function name without parentheses and internally evaluate it.

But for normal use I don't know of anything you can do to work around this built-in Excel behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM