views:

29

answers:

1

Hi friends,

In Macros coding, i am checking the not equal to condition. Values are in decimal or integer, Now i compare the two values in decimal values but not get the proper result. Please guide me what is the thing should be added for getting corrected result?

My code is :

 If fld4 <> fldval Then
    MsgBox "....."
 End If

But the output of message showing for the following values :

fld4 = 0.25, fldval = 0.26
fld4 = 0.25, fldval = 0.25
fld4 = 0.14, fldval = 0.14
fld4 = 0.11, fldval = 0.11
+2  A: 

Never ever (!) compare floating point values directly. Better try something like this:

 eps = =.00001 'choose an appropriate epsilon
 If Abs(fld4- fldval)<eps Then
   '...

Look here for further information.

Doc Brown
Thanks a lot. I just use Abs and got correct result. Now my code is If Abs(fld4) <> Abs(fldval) ThenIs it enough or anything to be added?
Karthik
If Abs used the way you show it gives you an answer, it suggests that you may have negative numbers: http://en.wikipedia.org/wiki/Absolute_value That is _not at all_ what Doc Brown shows. Depending on what the data refers to, you may find Round(nn.nn, nn) suits.
Remou
@Karthik: using Abs(fld4) <> Abs(fldval) normally won't work, that is an expression that still compares 2 floating point values - DON'T DO THIS!!!. You should really try it the way I wrote in my answer, and invest some time to read the article I gave you a link to.
Doc Brown
Ok Sure. So much Thanks... I go through that...
Karthik