PDA

View Full Version : Comparison Operator Funkiness



Soul777Toast
02-28-2008, 04:09 PM
Hi All,

I'm having a little wierdness with using the >= and <= comparison operators.

Can anyone tell me why these conditions

If StartTime.Value >= Range.StartTime And StartTime.Value <= Range.EndTime Then
MsgBox "Part Of This Range Has Already Been Selected. Please Select New Range", vbCritical
Unload Me
AddDataRangeForm.Show
Exit Sub
End If

would not trigger the if statements but these conditions

If StartTime.Value - Range.StartTime >= 0 And StartTime.Value - Range.EndTime <= 0 Then
MsgBox "Part Of This Range Has Already Been Selected. Please Select New Range", vbCritical
Unload Me
AddDataRangeForm.Show
Exit Sub
End If


work just fine with the same values for StartTime.Value, Range.StartTime and Range.EndTime (eg StartTime.Value = 5, Range.StartTime = 1, Range.EndTime = 10) in both instances?



Thanks Guys!

mikerickson
02-28-2008, 07:19 PM
Its not clear if the variables being compared are strings or numeric (Date, Double,..)
The comparison operaters work on both strings and numbers. In a mixed situation they will treat the number as a string. Which gives odd results.

By subtacting, Excel tries to convert the string to a number and succeds, giving the right result (in this case).

Awareness of what data type each variable represents and explicit conversion will prevent these problems.

BTW, TextBox1.Value is always a string.