mercredi 20 mai 2015

vba elseif evaluation error

I am doing VBA coding in Excel 2007.

i want to add 0.1 when tm is < 5 and add 0.25 when tm is at least 5, and some other cases. In the following code, when in 3rd iteration, the expression (tm < 5) is evaluated true when tm = 5. What is going on????

Even more interesting, if you try starting tm = 4.9, the evaluation would be correct!

    Sub test()

Dim i As Integer
Dim tm As Double
tm = 4.8

For i = 1 To 5

MsgBox tm & " " & (tm < 5)

    If tm < 2 Then
        tm = tm + 0.05
    ElseIf tm < 5 Then
        tm = tm + 0.1
    Else
        tm = tm + 1
    End If


Next i

End Sub

Aucun commentaire:

Enregistrer un commentaire