Announcement

Collapse
No announcement yet.

Truncated Double in VBA

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Truncated Double in VBA

    I am having trouble passing a string from Excel VBA to be converted and processed in PB 6.0, and then passing back a derived double precision value. Only the first digit of the string appears in the worksheet; "88.5C" for example should return 88.5, but only 8 is returned. Using the debugger shows that the internal conversion in PB 6 is perfect.

    This toy program shows the essentials of the problem:


    In Excel VBA:

    Option Explicit

    Declare Function CNVSTR Lib "Tryadd.dll" _
    (ByVal Coord AS STRING) AS DOUBLE

    $COMPILE DLL


    FUNCTION CNVSTR(s AS ASCIIZ) EXPORT AS DOUBLE
    DIM Result AS DOUBLE
    Result = VAL(s)
    CNVSTR = Result
    END FUNCTION

    Changing ASCIIZ to STRING and leaving out the ByVal does not help.

    I am aware of the care to be taken with strings between VBA and PB, but I am baffled why a double precison value derived from a passed string is getting truncated to one digit. Help!

    John Lenihan


  • #2
    John --
    Your functions works fine (at least on my PC).
    Probably, you have wrong formatting of Excel cells.
    Execute in macro MsgBox CNVSTR("88.5") and you will see that return value is 88.5

    ------------------

    Comment


    • #3
      Thanks for the reply. I thought about formatting, and did it, but there was no change except the single digit was formatted differently. That msgbox trick works on my PC too, but it still does not get into the worksheet. But this DOES get there perfectly:

      FUNCTION TIMESPI(X AS DOUBLE) EXPORT AS DOUBLE
      FUNCTION = X * 3.14159#
      END FUNCTION

      The worksheet has the correct number, decimals and all, perfect. Apparently there is only a problem when a string is passed and a double returned.

      Actually, this is not the first time I have had strange bugs with doubles in Excel VBA (one reason I want to use PB 6); I believe I must find a work-around.

      Best regards,
      John Lenihan



      ------------------

      Comment

      Working...
      X