如果条件有多个和条件

时间:2017-03-24 07:08:38

标签: excel-vba vba excel

我使用的代码包含多个If条件的And条件。

此代码正常工作,直到我没有使用多个And条件,但当我使用多个And条件时出现错误:

  

子或功能未定义。

即。当我将代码扩展到下一级作为代码行

代码如下: -

Sub main()

With Worksheets("Employee Data")

Sheets("Employee Data").Range("AQ4:AQ2000").Value = ""

If Range("F4") = "" Then
Range("AQ4") = ""
Else
If Range("F4").Value = "E1" And Range("AB4").Value = "Yes" Then
Range("AQ4").Formula = "=""Due for Promotion to E2A Level w.e.f."" &  TEXT(DATE(YEAR(AP4)+1,MONTH(AP4),DAY(AP4)),""DD-MMM-YYYY"")"
Else
If Range("F4") = "E1" And Range("AB4").Value = "No" Then
Range("AQ4").Formula = "=""Due for Promotion to E2A Level w.e.f."" &  TEXT(DATE(YEAR(AP4)+2,MONTH(AP4),DAY(AP4)),""DD-MMM-YYYY"")"
If Range("F4") = "E2" And Range("AB4").Value = "Yes" Then
Range("AQ4").Formula = "=""Due for Promotion to E2A Level w.e.f."" &  TEXT(DATE(YEAR(AP4)+1,MONTH(AP4),DAY(AP4)),""DD-MMM-YYYY"")"
Else
If Range("F4") = "E2A" And Range("AB4").Value = "Yes" Then
Range("AQ4").Formula = "=""Due for Promotion to E3 Level w.e.f."" &  TEXT(DATE(YEAR(AP4)+4,MONTH(AP4),DAY(AP4)),""DD-MMM-YYYY"")"

' error lies in this code line i.e. sub or function not defined

Else
If Range("F4") = "E2A" And Range("AB4").Value = "NO" And Ragne("P4").Value < 9 And Range("AE4").Value = "NA" Then
Range("AQ4").Formula = "=""Due for Promotion to E3 Level w.e.f."" &  TEXT(DATE(YEAR(AP4)+5,MONTH(AP4),DAY(AP4)),""DD-MMM-YYYY"")"
End If
End If
End If
End If
End If
End If
End With
End Sub

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我强烈建议您使用适当的缩进,并且可以使用@PreDestroy来避免过多ElseIf

(我知道这不是一个答案,但在评论中指出太多,而且由于拼写错误,问题可能会因此而被关闭)

End If