Consulting

Results 1 to 11 of 11

Thread: Solved: IF/ ELSEIF Help

  1. #1

    Solved: IF/ ELSEIF Help

    Hi all,

    The below code works ok up until the last elseif. Im trying to blank a cell out if the value of F13 is 0. F13 has data validation of 0 - 45. So F13 is always 0 until someone enters another number. Any ideas?

    thanks mucho grande


    [VBA]
    Sub Angelina()
    If Range("F13") >= 24 And Range("BX15") <> "A" Then
    Range("E13") = "A ONLY"

    ElseIf Range("BX$15") = "A" Then
    Range("E13") = Range("$CF$16")

    ElseIf Range("BX$15") = "B" Then
    Range("E13") = Range("$CF$17")

    ElseIf Range("BX$15") = "2" Then
    Range("E13") = Range("$CQ$16")

    ElseIf Range("BX$15") = "3" Then
    Range("E13") = Range("$CQ$17")

    ElseIf Range("BX$15") = "4" Then
    Range("E13") = Range("$CQ$18")

    ElseIf Range("BX$15") = "N" Then
    Range("E13") = Range("$DC$14")

    ElseIf Range("F13") = 0 Then
    Range("E13") = " "
    End If
    End Sub
    [/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What's in BX15?
    ____________________________________________
    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

  3. #3
    BX15 =

    A
    B
    2
    3
    4
    N

    Populated by a drop down box selection

  4. #4
    Heres a file. This example also wont populate a space if cell value = 0

    Not the example above but same result.

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I have not looked at the attachment yet, but what do you mean by not working for the last ElseIf? If the conditions are met, it enters a space in E13. If you want the cell to be blank, ditch (delete) the space you have between the quote marks, or, use ClearContents.
    [vba]
    Range("E13") = ""
    'or
    Range("E13").ClearContents[/vba]
    Does that help?

    Mark

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What are you on about? What does thread #4 have to do with the original question, and your answer in #3 doesn't tell me what is in BX15, just the options available.
    ____________________________________________
    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
    Xid,

    Sorry for confusion...to answer your first question..bx15 contains A.

    The second post was a different example with the same problem. Thought it would help to post a file.

  8. #8
    GTO,

    basically if the conditions in the statement arent met I want to put a blank space in cell E13. Your suggestions didnt do it...probably a logic problem with my brain :-)

  9. #9
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Unless I am missing something, the only time that the last ElseIf will execute is if there is nothing in BX15 AND F13 = 0

    To reiterate, if any of the choices available to BX15 have been made, you already have executed one of the preceeding ElseIf's.

    If I leave BX15 blank, and put a zero in F13, it does execute the last ElseIf.

    Is that the same for you?

    Mark

  10. #10
    GTO,

    Yes your right... Now I see whats happening. The condtion is met thus populating the cell before it gets to the end.

    Guess what im trying to do is add something that if cell F13 = 0 AND BX15 = A..populate E13 with a blank space....see posted code.

    Basically, upon opening the worksheet the default selection will be A and cell F13 will = 0. So I want cell E13 with a blank space.

    [vba]
    Sub Angelina()
    If Range("F13") >= 24 And Range("BX15") <> "A" Then
    Range("E13") = "A ONLY"

    ElseIf Range("BX$15") = "A" Then
    Range("E13") = Range("$CF$16")

    ElseIf Range("BX$15") = "B" Then
    Range("E13") = Range("$CF$17")

    ElseIf Range("BX$15") = "2" Then
    Range("E13") = Range("$CQ$16")

    ElseIf Range("BX$15") = "3" Then
    Range("E13") = Range("$CQ$17")

    ElseIf Range("BX$15") = "4" Then
    Range("E13") = Range("$CQ$18")

    ElseIf Range("BX$15") = "N" Then
    Range("E13") = Range("$DC$14")

    ElseIf Range("F13") = 0 Then
    Range("E13") = " "
    End If
    End Sub
    [/vba]

  11. #11
    Ok..this does it. Thanks for the help all. Now I understand IF's a bit better...


    [VBA]
    Sub Angelina()

    If Range("BX$15") = "A" And Range("F13") = 0 Then
    Range("E13") = " "

    elseIf Range("F13") >= 24 And Range("BX15") <> "A" Then
    Range("E13") = "A ONLY"

    ElseIf Range("BX$15") = "A" Then
    Range("E13") = Range("$CF$16")

    ElseIf Range("BX$15") = "B" Then
    Range("E13") = Range("$CF$17")

    ElseIf Range("BX$15") = "2" Then
    Range("E13") = Range("$CQ$16")

    ElseIf Range("BX$15") = "3" Then
    Range("E13") = Range("$CQ$17")

    ElseIf Range("BX$15") = "4" Then
    Range("E13") = Range("$CQ$18")

    ElseIf Range("BX$15") = "N" Then
    Range("E13") = Range("$DC$14")

    ElseIf Range("F13") = 0 Then
    Range("E13") = " "
    End If
    End Sub
    [/VBA]

Posting Permissions

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