bracketing "ifs" and "ands" and "ors" in VBA over Excel

How would I write this in VBA

if (code1 = 13 and answer1 = 15) or (code1 = 14 and answer1 = 16) then do ...... it seems not to like brackets

and if answer1 has several possbilities

if (((code1 = 13 and answer1 = 15) or answer1 = 20) or answer1 = 24) .... where do the brackets go 

Thanks again,

Hamish

 

 

 

 

 

 



Answer this question

bracketing "ifs" and "ands" and "ors" in VBA over Excel

  • doug finke

    The operator "AND" has a higher precedence than the operator "OR". Therefore, the "AND" operator will always be evalutated before the "OR" operator when there is no bracket.

    Hope this can help you.



  • Steven Syfuhs

    Hi

    If Answer1 has several numeric possiblities then consider using select case as an option eg.

    If Code1 =13 then

    select case answer1

    case1

    ....do something

    case 2

    ....do something

    case else

    end select

    else

    if code1 = 14 then

    ...... more code

    endif

    endif


  • Matt Falzon

    A "dialogue", to show a simple message with a couple of buttons, is the Msgbox function. Type Msgbox("<Your message>",<the type of buttons you want>) to display a pop-up box with some option buttons. Use it in an if statement like a function. Eg:

    If MsgBox("Do you want to continue ", vbYesNo) = vbYes Then
    <Your statements>
    End If

    see http://msdn2.microsoft.com/en-us/library/139z2azd.aspx for more on msgbox.

  • Lee Hesselden

    Nice. Thanks.

    What about displaying messages on screen that relate to a value netered by the user.

    Needs to hold 100 words or more in the message.

    Dialogues but how

    Thanks agina.

    H.


  • bracketing "ifs" and "ands" and "ors" in VBA over Excel