PDA

View Full Version : Solved: "erroneous" result with IF condition!!



Ctrl
11-04-2010, 10:59 AM
Hi

This code says the two values are not equal though they seem to be.

d1stValue should be equal to d2ndValue at the end of the procedure unless i am missing something!!

I put the if condition just to prove the result which is always "False".

Any help will be appreciated.

Excel 2003
XP SP3

Sub Macro1()

Dim iRow As Integer
Dim d1stValue As Double
Dim d2ndValue As Double

intRow = 1
d1stValue = 17.5
d2ndValue = 19.25

Do

With Cells(intRow, 1)
.NumberFormat = "0.00"
.Value = d1stValue
End With

If d1stValue >= d2ndValue Then Exit Do

Select Case d1stValue

Case Is >= 50
d1stValue = d1stValue + 0.25

Case Is >= 25
d1stValue = d1stValue + 0.1

Case Is < 25
d1stValue = d1stValue + 0.05

End Select

intRow = intRow + 1

Loop

Range("B1").Value = d1stValue
Range("C1").Value = d2ndValue

If d1stValue = d2ndValue Then

MsgBox "True"

Else

MsgBox "False"

End If

End Sub

Bob Phillips
11-04-2010, 11:04 AM
Probably due to floating point rounding.

Try



Sub Macro1()

Dim iRow As Integer
Dim d1stValue As Double
Dim d2ndValue As Double

intRow = 1
d1stValue = 17.5
d2ndValue = 19.25

Do

With Cells(intRow, 1)
.NumberFormat = "0.00"
.Value = d1stValue
End With

If d1stValue >= d2ndValue Then Exit Do

Select Case d1stValue

Case Is >= 50
d1stValue = d1stValue + 0.25

Case Is >= 25
d1stValue = d1stValue + 0.1

Case Is < 25
d1stValue = d1stValue + 0.05

End Select

intRow = intRow + 1

Loop

Range("B1").Value = d1stValue
Range("C1").Value = d2ndValue

MsgBox (d1stValue = d2ndValue) < 0.000001

End Sub

Ctrl
11-04-2010, 11:15 AM
Thank you xld

I tried your code but I'm not sure if I can get it.

The result is "False" too

How can i deal with floating point rounding and make sure the result will always be True?

19.25 = 19.25

I'm dealing with the numbers in this format "0.00"

Ctrl
11-04-2010, 11:34 AM
oops, sorry

actually, the result was "True". I ran my previous macro :doh:

but still need to apply this in the [do...loop] and [if..] conditions

Ctrl
11-04-2010, 11:40 AM
Oh, I found it.

If (d1stValue = d2ndValue) < 0.01 Then

MsgBox "True"

Else

MsgBox "False"

End If
Thank you for inspiring me xld

Bob Phillips
11-04-2010, 12:38 PM
No, my way is better, there is an implicit tetst in it



MsgBox (d1stValue = d2ndValue) < 0.01

Ctrl
11-04-2010, 01:23 PM
Yes, you are right. But I'm after the calculations and the logical tests where the very first two numbers at the right of the decimal are most important.

I just included the If.. condition and the message box in the macro as a proof for the "error" I had.

Ctrl
11-05-2010, 09:47 AM
I was having troubles again with that solution as I could not apply it to some conditions. But after I searched the web about working with floating point rounding I found something that did the trick and gave me the expected results. So the code now goes like this:

If Format(d1stValue, "0.00") >= Format(d2ndValue, "0.00") Then Exit Do
Likewise

If Format(d1stValue, "0.00") = Format(d2ndValue, "0.00") Then

MsgBox "True"

Else

MsgBox "False"

End If
So the Format function did the trick for me and now conditions are more logical than before.

regards