C-function in Excel through VBA : how ?

J

joris adriaenssens

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top