Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2006
    Posts
    103

    Unanswered: problem on double variable

    I have a problem on Double variable

    eg.

    dim d as double


    'while a, b, c is value from cell A1, A2, A3, eg. A1=1.5, A2=3.35 , A3=3.65

    d = a+b+c

    'now, when i compare the value

    if d<8.5 then
    Msgbox "d is less than 8.5"
    end if

    'the weird thing happen was, when d=8.5, it will go in to print out 'd is less than 8.5'

    but if i set d=8.5, not d=a+b+c, then it won't print that message, because d is not less than 8.5

    can anyone tell me why this is happening?

  2. #2
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1
    Hi

    Not sure what the problem is, but I get no message from either of these

    Code:
    Sub test1()
        Dim a As Double
        Dim b As Double
        Dim c As Double
        Dim d As Double
        
        a = Range("A1") '= 1.5
        b = Range("B1") '= 3.65
        c = Range("C1") '= 3.35
        d = a + b + c
        
        If d < 8.5 Then MsgBox "d is less than 8.5"
    
    End Sub
    
    
    Sub test2()
        Dim d As Double
        
        d = Range("A1") + Range("B1") + Range("C1")
        
        If d < 8.5 Then MsgBox "d is less than 8.5"
    End Sub

    ???


    MTB

  3. #3
    Join Date
    Jun 2006
    Posts
    103
    Thats what i think too, actually the program was written by previous programmer, however, only recently we found out this is happening, i tried to look into the problem, and found out that, the program tried to add a few double value a+b+c, then when i debug i can see the value is equal to 8.5

    then it went in a if statement

    if a+b+c < 8.5 then
    'inform the user they has entered a value less than 8.5

    but even a+b+c = 8.5 the computer still prompt the message, hm..i currently do not have time to remove some of the sensitive data and post the template here. (this is a timesheet input for all our company's staff, and probably i need time to remove those that need to remove b4 posting the template here)

    Thanks.

  4. #4
    Join Date
    Jun 2006
    Posts
    103
    oh...sorry, i posted twice for the same message, i removed the text here. Thanks.

  5. #5
    Join Date
    Oct 2003
    Posts
    1,091
    Quote Originally Posted by mkggoh
    I have a problem on Double variable

    eg.

    dim d as double


    'while a, b, c is value from cell A1, A2, A3, eg. A1=1.5, A2=3.35 , A3=3.65

    d = a+b+c

    'now, when i compare the value

    if d<8.5 then
    Msgbox "d is less than 8.5"
    end if

    'the weird thing happen was, when d=8.5, it will go in to print out 'd is less than 8.5'

    but if i set d=8.5, not d=a+b+c, then it won't print that message, because d is not less than 8.5

    can anyone tell me why this is happening?
    It may be that the numbers are actually rounded, and so the sum is not identical to what is seen as the sum of the numbers.
    old, slow, and confused
    but at least I'm inconsistent!

    Rich
    (retired Excel 2003 user, 3/28/2008)

    How to ask a question on forums

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •