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
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.
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
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 "".
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.