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
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?
Thank you
Fabio
Try this
Or better still... this
Note: I am assuming that
i
is a valid row numberExplanation:
When comparing using an
If
statement, you cannot sayIf A = B or C
. You are supposed to compare is separately.If A = B or A = C Then
. Here eachOR
is its ownBoolean
statement i.e it will be evaluated separately.When you have multiple such comparisons, it is better to use a Select Statement as shown in the example above.
Here are the alternatives:
You may use the Excel OR function as follows
or a slightly different approach from Siddharth Rout's answer using the benefit of the Select Case statement on text, especially for
"D-1"
,"D-2"
, and"D-3"
, to determine if the condition lies between other text in an alphabetical sense. Caution: this can at times cause unexpected results so make sure you test it beforehand.The last but not the least is to use For Each...Next statement of an array
One point that should be made here is that, by default, VBA code is case sensitive.