PDA

View Full Version : VBA if..then comparison not working ???



VTengineer
07-14-2008, 09:00 AM
Have been using VBA for 10+ years and I have no idea what's going on here. Look at the code below. The StopLong comparison in the first IF statement does not work. When I step through this in debug mode and StopLong reaches 0.085, it passes right over it. Logic involving the other two variables works just fine. I have change the value to different numbers and nothing works. I have renamed the variable, nothing works.

For BuyLim = 0.001 To 0.07 Step 0.002
For GainSell = 0.001 To 0.08 Step 0.003
For StopLong = 0.001 To 0.09 Step 0.003

If StopLong = 0.085 Then
If GainSell = 0.001 Then
If BuyLim = 0.001 Then
condition = true
End If
End If
End If

Next
Next
Next

RichardSchollar
07-14-2008, 09:06 AM
Hi

multiply all values by 1000 and use integers (ie whole numbers) for the boundaries and step values. I expect you won't have a problem then. It is most likely caused by Excel's limited precision with floating point numbers.

Richard

Bob Phillips
07-14-2008, 09:08 AM
It is just standrad FP inaccuracy. Test within limits, such



For BuyLim = 0.001 To 0.07 Step 0.002
For GainSell = 0.001 To 0.08 Step 0.003
For StopLong = 0.001 To 0.09 Step 0.003

If Abs(StopLong - 0.085) < 0.000000001 Then
If Abs(GainSell - 0.001) < 0.000000001 Then
If Abs(BuyLim - 0.001) < 0.000000001 Then
Condition = True
End If
End If
End If

Next
Next
Next