# If statement won't recognize two values as equal

1. ## If statement won't recognize two values as equal

Hi all,

I've included a detailed screen shot with some notes on it, I would encourage taking a look at that.
The issue I'm having is that a loop statement of mine is failing to recognize that a couple values are equal. It works for the first 3 iterations of the loop, then stops recognizing. I tried copying another set of numbers in the failing one's place, and that actually worked.
Not sure how else to explain it, but if you take a look at the screen shot you can see the excel sheet being worked on and the vba code running. I will also include a text file of the code for anyone who would like to change the code to offer a suggestion.

Thank you!

2. ## Re: If statement won't recognize two values as equal

It's hard to say for sure, but this sort of thing usually turns out to be some kind of round off error. Either there's formatting or a round function that is hiding some of the digits, or it is simple round off error out in the 10+ digit due to errors in double precision arithmetic.

Because round off errors are so common in any computer programming, my mentors almost never tested for "exactly equal to" like this. They would almost always do something like "is abs(a-b)<e" where e is some small number.

3. ## Re: If statement won't recognize two values as equal

Hey,

I got another reply on another forum a few moments ago, and that was exactly the issue.
The problem was corrected by changing the If statement to something like If Round(myVariable,2) = Round(myOtherVar) Then...

4. ## Re: If statement won't recognize two values as equal

This is a common problem with floating-point arithmetic. Do a Google search on this issue and you will find a lot written on this problem.

5. ## Re: If statement won't recognize two values as equal

P.S. See: "Floating-point arithmetic may give inaccurate results in Excel" at: http://support.microsoft.com/kb/78113

There are currently 1 users browsing this thread. (0 members and 1 guests)