用“或”语句嵌套“和”语句

时间:2013-07-17 20:11:07

标签: excel vba excel-vba

我的代码行给了我:错误13:类型不匹配 如何在“或”中组合“nd”状态? 例如:如果a或b或c或(d和e)或(f和g)

这是代码

If (cell.Value) = "FTV1" _
         Or (cell.Value) = "FTV2" _
         Or (cell.Value) = "FTV3" _
         Or (cell.Value) = "FTV4" _
         Or (cell.Value) = "FTV5" _
         Or (cell.Value) = "ISTCR" _
         Or (cell.Value) = "CAST" _
         Or (cell.Value) = "Rig" _
         Or (cell.Value) > 50000 And (cell.Value) < 52000 _
         Or (cell.Value) > 55000 And (cell.Value) < 56000 Then

提前致谢

2 个答案:

答案 0 :(得分:0)

将复合语句括在括号中,向编译器解释如何订购逻辑语句......

试试这个:

If (cell.Value) = "FTV1" _
         Or (cell.Value) = "FTV2" _
         Or (cell.Value) = "FTV3" _
         Or (cell.Value) = "FTV4" _
         Or (cell.Value) = "FTV5" _
         Or (cell.Value) = "ISTCR" _
         Or (cell.Value) = "CAST" _
         Or (cell.Value) = "Rig" _
         Or ((cell.Value) > 50000 And (cell.Value) < 52000) _
         Or ((cell.Value) > 55000 And (cell.Value) < 56000) Then

答案 1 :(得分:0)

如果没有在此语句中嵌套另一个AndSelect CaseIf/Then条件有点棘手,但假设您正在处理字符串或整数/长值,则应该工作(测试)。

Select Case cell.Value
    Case "FTV2", "FTV3", "FTV4", "FTV5", _
         "ISTCR", "CAST", "Rig", _
         50001 To 51999, 55001 To 55999

        'Do your code or call subroutine/function, here.

    Case Else

        'Do other code, or, do nothing, if the above criteria not met.

End Select

如果您正在使用双/十进制数据类型,那么我可能需要修改为(未经测试的):

Select Case cell.Value
    Case "FTV2", "FTV3", "FTV4", "FTV5", _
         "ISTCR", "CAST", "Rig", _
         50000 to 56000

         If (Not IsNumeric(cell.Value)) Or _
            (50000 < cell.Value < 52000) Or _
            (55000 < cell.Value < 56000) Then

            'Do your code or call subroutine/function, here.

         End If
    Case Else

        'Do other code, or, do nothing, if the above criteria not met.

End Select