Hi Federico,
You can try to return ExcelEmpty.Value from the scalar function, but that might not give you the expected result on the worksheet.
For a UDF called from a worksheet formula, Excel will never store the resulting value as "Empty". (This is independent of the technology you use to implement the UDF.)
So you need to decide what to return from your UDF:
* An empty string
* An error value (e.g. ExcelError.ExcelErrorValue)
* ExcelEmpty.Value - which Excel will then convert to some other value on the worksheet.
If you return "Nothing" (null) from a UDF, the Excel-DNA marshaling is not called for the resulting value, but Excel always interprets the "Nothing" (null) value as ExcelError.ExcelErrorNA. We have not control over that.
If you return "Nothing" (null) for an element in a result array, then Excel-DNA does convert the value to an Excel "Empty" type (equivalent to setting it to ExcelEmpty.Value), but Excel will convert the value to something else (since Excel can't store "Empty" as the result of a function). So to have consistent results for scalar and array functions where you control the result, you could return ExcelEmpty.Value in all cases.
Now the conversion from a UDF result into VBA might behave a bit differently, but is still not under control of Excel-DNA - if you call Application.Run("MyUDF", param1) then the result is first interpreted by Excel, then converted to a variant in VBA. You're saying you get the Empty variant in VBA in this case, but it is outside the control of Excel-DNA how the values are interpreted. You can try to return ExcelEmpty.Value from the scalar UDF to see whether Excel makes the same conversions.
Regards,
Govert