Consulting

Results 1 to 2 of 2

Thread: Comparison Operator Funkiness

  1. #1
    VBAX Regular
    Joined
    Feb 2008
    Location
    Lebanon NH
    Posts
    14
    Location

    Comparison Operator Funkiness

    Hi All,

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

    Can anyone tell me why these conditions
    [VBA]
    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
    [/VBA]
    would not trigger the if statements but these conditions
    [vba]
    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
    [/vba]

    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!
    ' Never Ending Case-o-Beer!
    Dim Beer as Variant
    Dim Mouth as String
    ReDim Case(0) as String
    Case(0) = "Beer"

    For Each Beer in Case
    Mouth = Beer
    ReDim Case(Ubound(Case) +1)
    Case(Ubound(Case)) = "Beer"
    Next Beer

    'Continue until system crash

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •