Reputation: 159
I have an If formula as part of my macro code which doesn't seem to be working in the correct manner.
The below code calculates the rCell.Offset and always applies it as being True, I have had examples where it equals 112, 113, 118 etc and it is populating the desired cell as Option 2.
If rCell.Offset(0, -2).Value = "101" Or "102" Then
rCell.Value = "Option 2"
Else
rCell.Value = "Option 1"
End If
Am I missing something?
Upvotes: 2
Views: 88
Reputation:
I'm going out on a limb and say that the numbers in rCell.Offset(0, -2) are actual numbers and not text-that-looks-like-a-number. Remember that 101 <> "101"
so remove the quotes as well as correct the Or syntax.
If rCell.Offset(0, -2).Value2 = 101 Or rCell.Offset(0, -2).Value2 = 102 Then
rCell.Value = "Option 2"
Else
rCell.Value = "Option 1"
End If
Another option would be a Select Case statement.
Select Case rCell.Offset(0, -2).Value2
Case 101, 102
rCell.Value = "Option 2"
Case 112, 113
'do something for these
Case 118
'do something for this
Case Else
'when nothing else matches
rCell.Value = "Option 1"
End Select
The benefits to this style of condition evaluation becomes quickly apparent when there are several conditions to check.
Upvotes: 1
Reputation:
Yes. Try this
If rCell.Offset(0, -2).Value = "101" Or rCell.Offset(0, -2).Value = "102" Then
EDIT: Essentially your code is asking
If "102"
which will always return true.
Upvotes: 5