Troubleshooting DLL function call from Excel Vba

carrie88

I'm trying to call a function from a DLL from VBA in Excel.

My Excel VBA macro looks like this:

Declare PtrSafe Function TestFunction1 Lib "mylib.dll" (ByVal k As Double) As Double

Public Function TestDll(k As Double) As Double
    Debug.Print ("Start")

    Dim r As Double
    r = TestFunction1(k)

    Debug.Print ("Got result of " + r)
    Debug.Print ("Done")

    TestDll = r
End Function

Now when I call it from an Excel cell with something like "=TestDll(3.0)", it doesn't work. I see the "Start" string in the immediate window, but nothing else. It's like an error is happening exactly when "TestFunction1" is being called. Excel displays "#VALUE!" in the cell.

I can also set a breakpoint in the debugger, but when I get to the TestFunction1 call, it just ends. There is no sort of error message I can find.

My question is, how do I debug this? I'm not getting any error message. It simply doesn't work. How can I figure out what is going wrong?

teddy2

The variable which you are using in debug statement,has an error and hence the UDF fails. Rest is fine. Actually you need to convert r to string or use & for concatenation in your debug statement.

Edit: to include error handler.

Public Function TestDll(k As Double) As Double
    Debug.Print ("Start")

    Dim r       As Double

    '/ Add a error handler
    On Error GoTo errHandler
    '/ Assuming that your testfunction will return 10*parameter
    r = k * 10


    '/ The variable which you are returning,has a error and hence the UDF fails.
    '/ Rest is fine. Here you will get type mismatch error.
    Debug.Print ("Got result of " + r)

    '/ Actually you need to convert it to string or use `&` for concatenation
    Debug.Print ("Got result of " + CStr(r))

    '/ or
    Debug.Print ("Got result of " & r)


    Debug.Print ("Done")

    TestDll = r

errHandler:
    If Err.Number <> 0 Then
        '/ Error trapped and you get actual error desc and number.
        MsgBox Err.Description, vbCritical, Err.Number
    End If

End Function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Troubleshooting Excel VBA Code

From Dev

Unable to call VBA function from Excel Worksheet Cell

From Dev

xlwings call from Excel VBA function produces error

From Dev

python call function from DLL

From Dev

Excel VBA Call Function and Loop

From Dev

Can I call a 64bit DLL from a 32bit version of Excel VBA?

From Dev

Calling functions from Rust DLL with Excel VBA

From Dev

DLL function not working in a VBA environment but working in Excel VBA

From Dev

Not able to call the function from C DLL

From Dev

Call .dll function from C# Ironpython

From Dev

Call a member function with offset address from Dll

From Dev

Can not call a function from a C# DLL

From Dev

Call C dll function from C#

From Dev

How to call function in my dll from jquery

From Dev

How (or is it possible) to call VBA (sub or function) from ribbon added to Excel through VSTO

From Dev

Call Scala Functions from Excel/VBA

From Dev

call excel 2010 vba from bash menu

From Dev

call excel 2010 vba from bash menu

From

How to call .NET methods from Excel VBA?

From Dev

How to call a C DLL from a VBA application and return an array?

From Dev

Call to undefined function sqlsrv_connect() - Troubleshooting

From Dev

VBA Match Function & Nested For Loops Troubleshooting

From Dev

VBA Range methods not working from function call

From Dev

How to call VBA function from batch file

From Dev

Changing function call from static to dynamic VBA

From Dev

Return String array from C++ DLL to VBA(Excel)

From Dev

DLL function call is not working

From Dev

Excel VBA using Range returned from Function

From Dev

Remove delimiters from Join() Function in EXcel VBA

Related Related

HotTag

Archive