我在下面的代码中收到错误“ else if if”。 有人可以帮忙吗?
For I = 2 To LR5
If (Range("Ae" & I).Value = 5 & Range("x" & I).Value = 2 & Range("y" & I).Value = 2) Then Range("ah" & I).Value = "30000"
ElseIf (Range("Ae" & I).Value = 5 & Range("x" & I).Value = 4 & Range("y" & I).Value = 4) Then Range("ah" & I).Value = "60000"
ElseIf Range("Ae" & I).Value = "29" Then Range("ah" & I).Value = "10000"
ElseIf Range("Ae" & I).Value = "30" & Range("x" & I).Value = "4" Then Range("ah" & I).Value = "80000"
ElseIf Range("Ae" & I).Value = "30" & Range("x" & I).Value = "2" Then Range("ah" & I).Value = "50000"
ElseIf Range("Ae" & I).Value = "17" & Range("x" & I).Value = "2" Then Range("ah" & I).Value = "60000"
ElseIf Range("E" & I).Value = "RRUS11" Then Range("AH" & I).Value = "80000"
ElseIf Range("E" & I).Value = "RRUS11+RRUSA2" Then Range("AH" & I).Value = "80000"
ElseIf Range("E" & I).Value = "RRUS12" Then Range("AH" & I).Value = "120000"
End If
Next
答案 0 :(得分:2)
Then
之后的所有内容移到新行。在VBA中,单行If
语句具有以下特征:
End If
,从未。例如:
If someCondition Then DoSomething
If someCondition Then DoSomething Else DomeSomethingElse
If someCondition Then DoSomething(x) Else If anotherCondition Then DoSomething(y) Else DoSomething(z)
但是,单行if
仅用于简短语句。通常,当您拥有Else
(更不用说Else If
)时使用它不是一个好主意。因此,在上面的代码块中,不建议使用第二个和第三个示例。相反,您应该这样编写If
语句:
If someCondition Then
DoSomething (x)
Else If anotherCondition Then
DoSomething (y)
Else
DoSomething (z)
End If
1 当然,您可以使用_
将语句分成多行。有关更多信息,请参见this。
答案 1 :(得分:0)
in a boolean expression you want to use AND
instead of &
furthermore in such a convoluted case a Select Case
block is much more readable:
For I = 2 To LR5
Select Case True
Case Range("Ae" & I).Value = 5 And Range("x" & I).Value = 2 And Range("y" & I).Value = 2
Range("ah" & I).Value = "30000"
Case Range("Ae" & I).Value = 5 And Range("x" & I).Value = 4 And Range("y" & I).Value = 4
Range("ah" & I).Value = "60000"
Case Range("Ae" & I).Value = "29"
Range("ah" & I).Value = "10000"
Case Range("Ae" & I).Value = "30" And Range("x" & I).Value = "4"
Range("ah" & I).Value = "80000"
Case Range("Ae" & I).Value = "30" And Range("x" & I).Value = "2"
Range("ah" & I).Value = "50000"
Case Range("Ae" & I).Value = "17" And Range("x" & I).Value = "2"
Range("ah" & I).Value = "60000"
Case Range("E" & I).Value = "RRUS11"
Range("AH" & I).Value = "80000"
Case Range("E" & I).Value = "RRUS11+RRUSA2"
Range("AH" & I).Value = "80000"
Case Range("E" & I).Value = "RRUS12"
Range("AH" & I).Value = "120000"
Case Else
' put code to handle the case when no preceeding conditions are met
End Select
Next
and here's another way of translating the same If Then Else If
of yours into a Select Case
block:
For I = 2 To LR5
Select Case Range("Ae" & I).Value
Case 5
Select Case True
Case Range("x" & I).Value = 2 And Range("y" & I).Value = 2
Range("ah" & I).Value = "30000"
Case Range("x" & I).Value = 4 And Range("y" & I).Value = 4
Range("ah" & I).Value = "60000"
End Select
Case "29"
Range("ah" & I).Value = "10000"
Case "30"
Select Case Range("x" & I).Value
Case "4"
Range("ah" & I).Value = "80000"
Case "2"
Range("ah" & I).Value = "50000"
End Select
Case "17"
If Range("x" & I).Value = "2" Then Range("ah" & I).Value = "60000"
Case Else
Select Case Range("E" & I).Value
csse "RRUS11", "RRUS11+RRUSA2"
Range("AH" & I).Value = "80000"
Case "RRUS12"
Range("AH" & I).Value = "120000"
Case Else
' you may want to put code to handle the case when no preceeding condition is met
End Select
End Select
Next