Consulting

Results 1 to 7 of 7

Thread: Help with "OR" in VBA

  1. #1

    Red face Help with "OR" in VBA

    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.

  2. #2
    when you are comparing to not,use AND, otherwise it will always be = to not the other criteria

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    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

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Select Case UCase(Trim(Range("C" & counter).Value))
       Case "C"
          'nothing
       Case "D"
          'nothing
       Case ""
          'nothing
       Case Else
           CDstr = CDstr & counter & ", "
    End Select
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by SamT View Post
    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
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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 "".
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •