5

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?

9
  • You are never assigning a value to r. And you are never declaring ret as Double. (Not sure whether those are causing your issues, but I suggest you fix them anyway.) Commented Sep 5, 2016 at 0:42
  • 3
    You can try adding the .dll file to the VBA project references and F2 to search for the method in the Object Explorer. Another thing that you can try is to use As Object instead of As Double. I don't think we can help you much without any information about mylib.dll and TestFunction1 Commented Sep 5, 2016 at 0:45
  • @YowE3K Sorry, I confused r with ret. They should have been the same. Fixed it. Commented Sep 5, 2016 at 0:51
  • This is a well-phrased question, but without the DLL and TestFunction1, it is not really something that anyone is likely able to assist with, beyond very high-level advice. Otherwise, the question's scope is limited to a very unique problem (i.e., limited to your specific DLL & function) and may not be very useful for the rest of SO community. Commented Sep 5, 2016 at 1:35
  • 1
    I gave you the code to trap the error. :) Commented Sep 5, 2016 at 1:47

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

5 Comments

THis is good advice regarding the & vs + for concatenation, the latter will yield a Type Mismatch error in this case. However, the root of the problem seems to be with the call to TestFunction1: when I get to the TestFunction1 call, it just ends.
Thanks for your answer. When I run in the debugger, execution doesn't go past " r = TestFunction1(k)".
see the edit to my answer. It will help you to trap the actual error. Post the error message and error number. May be some one could be able to help.
@teddy2 The "On Error" part is exactly what I needed! I have an actual error message to go on now, and hopefully I can get it from here. Thanks x1000! (I'd upvote you, but apparently I can't?)
great. dont worry about upvotes. As long as it works for you I am happy. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.