Consulting

Results 1 to 17 of 17

Thread: Solved: double case statement

  1. #1

    Solved: double case statement

    hey everyone newbs back,

    Simon thanks for your help from before (works) however I found out that there is another variable I need to check now...

    does the select case function have a syntax for checking 2 variables?

    heres what is going on: followed on from this thread


    [vba]
    sub test1()

    Dim Rng As Range, MyCell As Range

    Set Rng = Range("M3:M50")
    For Each MyCell In Rng

    Select Case MyCell.Value
    Case "fred"
    MyCell.Offset(0, -8).Value = "Entry Found"
    Case "bob"
    MyCell.Offset(0, -8).Value = "Entry Found1"
    End Select

    Next MyCell

    End Sub
    [/vba]
    how can I add a check for a value in range(O3:O50) which is either going to be NA or AD ? I need to get bob with NA to output one thing and bob with AD to output another. I cant seem to find the info anywhere!

    thanks!

  2. #2
    Try:

    [VBA]Sub test1()
    Dim Rng As Range, MyCell As Range
    Set Rng = Range("M3:M50")
    For Each MyCell In Rng
    Select Case MyCell.Value
    Case "fred"
    MyCell.Offset(0, -8).Value = "Entry Found"
    Case "bob"
    If MyCell.Offset(0, 2).Value = "NA" Then
    MyCell.Offset(0, -8).Value = "Bob NA"
    Else
    MyCell.Offset(0, -8).Value = "Bob AD"
    End If
    End Select
    Next MyCell
    End Sub[/VBA]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub test1()

    Dim Rng As Range, MyCell As Range

    Set Rng = Range("M3:M50")
    For Each MyCell In Rng

    With MyCell

    Select Case True

    Case .Value = "fred"
    .Offset(0, -8).Value = "Entry Found"
    Case .Value = "bob" And .Offset(0, 2).Value = "NA"
    .Offset(0, -8).Value = "Entry Found bob NA"
    Case .Value = "bob" And .Offset(0, 2).Value = "AD"
    .Offset(0, -8).Value = "Entry Found bob AD"
    End Select
    End With
    Next MyCell

    End Sub
    [/vba]
    ____________________________________________
    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

  4. #4
    ok I tried both, runs through, populates the freds but doesnt populate the bobs (does the same thing for both)

    also doesnt give me any errors... thoughts?

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not without seeing the data.
    ____________________________________________
    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

  6. #6
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    Do you need four possible cominations?

    bob na
    bob ad
    fred na
    fred ad

    output = fred + na = ({1,2,3,4})

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Sleepy, my thoughts are: your data isn't laid out as you explained and its possible you haven't added the text exactly as shown on your sheet in vba "bob" is not the same as "Bob" or "BOB"
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  8. #8
    hey guys, hope your weekend was good, the text is all in caps and i just double checked and it doesnt run any of the the ones that check for NA or AD.

    heres the code so far:

    [vba]Dim Rng As Range, MyCell As Range

    Set Rng = Range("M3:M50")
    For Each MyCell In Rng

    With MyCell

    Select Case True

    Case .Value = "fred"
    .Offset(0, -8).Value = "AD-freddata"

    Case .Value = "fred2"
    .Offset(0, -8).Value = "AD-freddata2"

    ' the above works fine, the below populates nothing.

    Case .Value = "bob" And .Offset(0, 2).Value = "NA"
    .Offset(0, -8).Value = "NA\bob"
    Case .Value = "bob" And .Offset(0, 2).Value = "AD"
    .Offset(0, -8).Value = "AD\bob"

    etc etc etc


    End Select
    End With
    Next MyCell[/vba]
    Last edited by sleepy_think; 05-12-2008 at 11:51 AM.

  9. #9
    And .Offset(0, 2).Value = "NA"

    is what is failing, is everyone sure that you can run case statements with the And syntax?

  10. #10
    got it! its not

    Case .Value = "bob" And .Offset(0, 2).Value = "NA"
    .Offset(0, -8).Value = "NA\bob"

    its

    Case .Value = "bob", .Offset(0, 2).Value = "NA"
    .Offset(0, -8).Value = "NA\bob"

    stupid "and" was killing it.

  11. #11
    Careful sleepy, more testing is needed. You're getting a false hit.

    Here, try this code to convince yourself:

    [vba]
    Sub CaseAnd()
    Dim x As Integer, y As Integer

    x = 2
    y = 4
    Select Case True

    Case x = 2, y = 2 'the wrong way, not true but gives a hit
    msgbox "False Hit"

    Case x = 2 and y = 2 'no false hit
    msgbox "False"

    Case x = 2 and y = 4 'this is what you want!!!
    msgbox "True"

    Case Else
    msgbox "Miss" 'catch all

    End Select
    End Sub[/vba]
    Vow this day in to the grounds of ergo post proctor hoctor, vis-a-vis telemundo.

  12. #12
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Sleepy i think its about time you posted a workbook for all these helpful people to see what you are working with and trying to achieve!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  13. #13
    mattkelin,

    I tried to fool around with your code but it just outputted blank cells... I added the "," isntead of the "and" (which doesnt seem to do anything) and it outputs fine and correctly.

    Simon, Id like to post this but I'd get fired so I wont :P I do thank everyone for there help through

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    "," is an OR condition whereas you originally said you wanted AND, which is what I gave you.
    ____________________________________________
    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

  15. #15
    but "and" keeps returning blanks.

  16. #16
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Sleepy, you need to create a dummy workbook with the exact layout of data, the data needn't mean anything just as long as the layout and format are the same that way all the people giving up their valuable free time here may be compelled to help you further.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  17. #17
    wow im a noob,

    figured it all out

    xld's code is right what was wrong was the offset on the NA and AD, when I chaged them to 0,1 it ran fine!

    thats why im the noob :P

    thanks again everyone.

Posting Permissions

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