PDA

View Full Version : Interface EXCEL-VBA with C-Sharp or C++ DLL



Janyh
01-29-2018, 08:22 AM
Hello Everyone,

I am new in VBA and programming as well :)
I am trying to connect/interface data placed in cells in Excel-VBA with C-sharp DLL or C++ DLL file. I have 2 DLL files. One is in C-Sharp and second one in C++.
Basically, DLL file contains functions/equations, procedures, classes and etc. that are calculating output values based on input values.
Main idea is to send Input values placed in EXCEL-VBA to DLL file, after that give command DLL to do calculation and finally return output values to excel cells again.

Firstly, I have only tried to interface function stored in DLL, but it does not work.

Here is code in VBA that should be able to interface with DLL:
Private Declare Function cp Lib "C:\calc.dll" (x As Double) As Double

Function cp_vba(x As Double) As Double

cp_vba = cp(x)

End Function

Here is function writtten in C-Sharp, that should receive x value from Excel and return cp value to Excell as well:

public static double cp(double x)
{
if (x == -1.0)
{
throw new DivideByZeroException("x");
}
return (1000.0 + x * 1810.0) / (1.0 + x);
}


Can someone help why If I insert input value x, the result of cp value is not received?
I can see in excell cell #HODNOTA. See attachement as well.

Any advice how to interface VBA with DLL?

many thanks in advance,
Regards,
Janyh