帮忙看看VB判断的问题

来源:百度知道 编辑:UC知道 时间:2024/06/05 11:32:47
小弟初学vb,写了这么一段语句:
Dim i As Double
i = ActiveCell.Range("A1")
If i <= 0.25 Then
Nodei = 1
ElseIf 0.25 < i <= 0.5 Then
Nodei = 2
ElseIf 0.5 < i <= 0.667 Then
Nodei = 3
ElseIf 0.667 < i <= 1 Then
Nodei = 4
ElseIf 1 < i <= 1.5 Then
Nodei = 5
ElseIf 1.5 < i <= 2 Then
Nodei = 6
ElseIf 2 < i <= 3 Then
Nodei = 7
ElseIf 3 < i <= 4 Then
Nodei = 8
ElseIf i >= 4 Then
Nodei = 9
End If

ActiveCell.Offset(0, 2).Range("A1").Select
ActiveCell.FormulaR1C1 = Nodei
可是无论在ActiveCell.Range("A1")
输入什么,最后运行结果都是1或者2,麻烦大侠帮看看。

v

判断的语法不对,应该ElseIf 3 < i and i<= 4 Then。不过这么多分支,建议用select case来写:
Dim i As Double
i = ActiveCell.Range("A1")
select case i
case is <= 0.25:Nodei = 1
case 0.25 to 0.5 :Nodei = 2
case 0.5 to 0.667 :Nodei = 3
case 0.667 to 1 :Nodei = 4
case 1 to 1.5 :Nodei = 5
case 1.5 to 2 :Nodei = 6
case 2 to 3 :Nodei = 7
case 3 to 4:Nodei = 8
case is >= 4 :Nodei = 9
End select

ActiveCell.Offset(0, 2).Range("A1").Select
ActiveCell.FormulaR1C1 = Nodei