PDA

View Full Version : Help with "OR" in VBA



finesaqib
01-18-2015, 03:51 AM
If UCase(Trim(Range("C" & counter).Value)) <> "D" Then
CDstr = CDstr & counter & ", "
End If

Obviously Above code checks "If statement is not equal to "D", Then"
I want to make it as "If statement is not equal to "C" or "D",Then"

PS: I dont have any knowledge of this. Must be simple.
Thanks for helping. :hi:

westconn1
01-18-2015, 04:00 AM
when you are comparing to not,use AND, otherwise it will always be = to not the other criteria

GTO
01-18-2015, 04:13 AM
Does this help?


If Not (UCase$(Trim(Range("C" & counter).Value)) = "C" Or UCase$(Trim(Range("C" & counter).Value)) = "D") Then
CDstr = CDstr & counter & ", "
End If


Mark

Bob Phillips
01-18-2015, 04:41 AM
You could also use something like


If UCase$(Trim(Range("C" & counter).Value)) = "C" Or UCase$(Trim(Range("C" & counter).Value)) = "D" Then
'do nothing
Else
CDstr = CDstr & counter & ", "
End If

I sometimes use this approach when I want to make the code state cxlearly what it is I am checking.

SamT
01-18-2015, 04:55 AM
Select Case UCase(Trim(Range("C" & counter).Value))
Case "C"
'nothing
Case "D"
'nothing
Case ""
'nothing
Case Else
CDstr = CDstr & counter & ", "
End Select

Bob Phillips
01-18-2015, 10:19 AM
Select Case UCase(Trim(Range("C" & counter).Value))
Case "C"
'nothing
Case "D"
'nothing
Case ""
'nothing
Case Else
CDstr = CDstr & counter & ", "
End Select


Why wouldn't you combine the do nothing cases?


Select Case UCase(Trim(Range("C" & counter).Value))
Case "C", "D"
'nothing
Case Else
CDstr = CDstr & counter & ", "
End Select

SamT
01-18-2015, 11:16 AM
Why wouldn't you combine the do nothing cases?
I would, but why not demonstrate to the newbie the basic usage of Select Case?

I also took the opportunity to hint that Case Else includes the case of an empty cell if not otherwise specified.

I hope that is a clue for the OP that [If Not "D"] also includes "".