I have a C++ function that I use in Excel 2013 through a DLL built with VC2013:
double my_function(double input) {
//do something
return input*input;
}
In Excel VBA I include this function like this:
Declare Function my_function Lib "DLL_with_my_function.dll" (ByVal input As Double) As Double
This works well so far, however, now, I'd like to be able to return a second piece of information, say an error code, through a second argument. Ideally this error code would have the ability to be output to a cell in Excel, or at least to the console through debug.print . I am stuck at making the whole thing work and have had Excel crash several times. This is my fruitless attempt:
double my_function(double input, long *error_code) {
*error_code = 5;
return input*input;
}
#in Excel:
Declare Function my_function Lib "DLL_with_my_function.dll" (ByVal input As Double, ByRef error_code as long) As Double
When I call the function from the worksheet and indicate a cell as the 2nd argument Excel crashes at me. What is the correct, elegant way to do this?
You just cant give exel cell as long number to c\c++, since it doesn't automatically cast
You can do this:
in Excel declare the new function too: