ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   C-function in Excel through VBA : how ? (https://www.excelbanter.com/excel-programming/338322-c-function-excel-through-vba-how.html)

Joris Adriaenssens

C-function in Excel through VBA : how ?
 
Hello,

I have written a function in C that calculates a double through an
iterative process (I found VBA too slow for it).

By adding the declaration to my function in VBA, the function becomes
available as a User Defined Function :

Public Declare Function afschrijving _
Lib "C:\Program Files\Microsoft Office\Office\air.dll" _
Alias "_afschrijving@52" _
(ByVal aankoopwaarde As Double, _
ByVal levensduur As Long, _
...
) as Double

Is this possible by just moving the dll in the right directory and
registering the function somewhere somehow when Excel starts ? The
calculation is still slow, even when I simplify the function to an
addition of 2 variables. I suspect that by going through VBA and a
User Defined Function, it slows down the calculation.

When my function makes a division by zero, I would like to communicate
an error to Excel (now I return zero as answer, which is wrong, I want
to have #DIV/0 in my worksheet).
I can put the answer in an XLOPER-structure. When there is no answer
but an error-message, I can put it in the XLOPER-structure as well.
But how do I declare such a function in VBA ?

Public Declare Function afschrijving _
Lib "C:\Program Files\Microsoft Office\Office\air.dll" _
Alias "_afschrijving@52" _
(ByVal aankoopwaarde As Double, _
ByVal levensduur As Long, _
... , _
ByRef answer As XLOPER _
) as Long

The Long that is returned becomes useless, the answer is now in the
XLOPER-structure (though I don't believe that this works : how do I
create a reference to an XLOPER-structure in VBA), but how do I declare
a function 'VOID' in VBA ?

A lot of questions, is there someone who can point me to a book or a
webpage that explains these things ?

With kind regards
Joris Adriaenssens



All times are GMT +1. The time now is 01:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com