运行时错误'13':IF上的类型不匹配或OR语句 [英] Run-time error '13': Type mismatch on IF combined with OR statement
问题描述
在使用excel的VBA代码中,我有
In my VBA code using excel I have
Dim Field2 As String
Field2 = Cells(i, 4).Value
If Right(Field2, 3) = ("A-1" Or "A-2" Or "B-1" Or "B-2" Or "C-1" Or "C-2" Or "D-1" Or "D-2" Or "D-3") Then
Cells(i, 50).Value = "OtherPrtnrs /Dcrs & Dept heads"
End If
运行代码时,出现以下消息: 运行时错误'13':类型不匹配"
An when I run the code, I get the follow message: "Run-time error '13': Type mismatch"
您是否知道如何使此代码正常工作?
Do you have any idea how to make this code work?
谢谢
Fabio
推荐答案
尝试一下
If Right(Field2, 3) = "A-1" Or _
Right(Field2, 3) = "A-2" Or _
Right(Field2, 3) = "B-1" Or _
Right(Field2, 3) = "B-2" Or _
Right(Field2, 3) = "C-1" Or _
Right(Field2, 3) = "C-2" Or _
Right(Field2, 3) = "D-1" Or _
Right(Field2, 3) = "D-2" Or _
Right(Field2, 3) = "D-3" Then
Cells(i, 50).Value = "OtherPrtnrs /Dcrs & Dept heads"
End If
或者更好...这个
Select Case Right(Field2, 3)
Case "A-1","A-2","B-1","B-2","C-1","C-2","D-1","D-2","D-3"
Cells(i, 50).Value = "OtherPrtnrs /Dcrs & Dept heads"
End Select
注意:我假设i
是有效的行号
Note: I am assuming that i
is a valid row number
说明:
使用If
语句进行比较时,不能说If A = B or C
.您应该比较是分开的. If A = B or A = C Then
.每个OR
都是其自己的Boolean
语句,即将分别进行评估.
When comparing using an If
statement, you cannot say If A = B or C
. You are supposed to compare is separately. If A = B or A = C Then
. Here each OR
is its own Boolean
statement i.e it will be evaluated separately.
当您有多个这样的比较时,最好使用如上例所示的Select语句.
When you have multiple such comparisons, it is better to use a Select Statement as shown in the example above.
这篇关于运行时错误'13':IF上的类型不匹配或OR语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!