BruceWayne
BruceWayne

Reputation: 23283

Is value X within a range?

I have a number, say 1535.25 stored as variable possibleDup, that I'm trying to compare to another number, say 1535 that is the variable realNumber. Both of these will be the same number, only one is going to be rounded/truncated (realNumbertends to be truncated, so I can't do a straight numerical comparison).

I'm trying to see if my possibleDup number is within 1 of the realNumber. When I do If possibleDup = realNumber Then ..., it will seldom run, since one tends to have decimals, and the other not.

Normally, I'd do If (realNumber + 1) >= possibleDup >= (realNumber - 1) Then ..., but you can't set up a logic statement like that in VB (AFAIK).

(Alternative ideas are welcome. I have also tried doing If left(possibleDup,search(".",possibleDup)-1) = realNumber but my possibleDup won't always have a decimal, so I'd have to lengthen my code to account for that).

Maybe setting these values as String instead of Long and doing a search within the string?

Upvotes: 0

Views: 53

Answers (1)

John Kugelman
John Kugelman

Reputation: 361605

You need to use the And operator to chain the conditions of your boolean test:

If (realNumber + 1) >= possibleDup And possibleDup >= (realNumber - 1) Then

In this specific case, you could do this:

If Abs(realNumber - possibleDup) <=1 Then

Upvotes: 1

Related Questions