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

Vba 用“或”语句嵌套“和”语句,vba,excel,Vba,Excel,我的这行代码告诉我:错误13:类型不匹配 如何在or中组合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.Val

我的这行代码告诉我:错误13:类型不匹配 如何在or中组合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

提前感谢

将复合语句括在额外的括号中,向编译器解释如何对逻辑语句进行排序

试试这个:

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
如果不在该语句中嵌套另一个Select Case或If/Then,则And条件有点棘手,但假设您处理的是字符串或整数值/长数值,则应该可以测试

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

使用括号将复合语句括在所有这些cell.Value中,而不是简单的cell.Value???似乎最好使用Select Case语句。。我该如何写一个案例陈述呢?抱歉耽搁了,我已经离线了。我将立即发布一个建议。假设单元格是一个范围,当它不工作时,它的值是多少?当这是错误的时候?任何其他随机的东西。我只是把它作为范围插入函数S_O_TestByRef cell中,得到了所有预期结果以及有效和无效的输入。之后,我有一个S_O_测试=1,另一个返回S_O_测试=0。这个答案有什么问题?谢谢你指出。我仔细检查了所有的盒子,发现其中一个盒子上有N/A,这是不应该的。这就是错误所在