Results 1 to 4 of 4

Thread: [RESOLVED] If ... AND ... then problems

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2010
    Posts
    19

    Resolved [RESOLVED] If ... AND ... then problems

    So Ive set up a simple if else if statement with an "Or" and a "AND" operator.

    vb Code:
    1. Set TempRange = Blad2.Range(BoutMBegin, BoutMEind)
    2.     For Each Cell In TempRange
    3.         If Cell.Value = b_grade Then
    4.             Blad2.Range(BoutMBegin).Select
    5.             ActiveCell.Offset(-1, 1).Select
    6.             bma = 0
    7.             bmd = 0
    8.             bmi = 1
    9.             Do While Not IsEmpty(ActiveCell)
    10.                 If ActiveCell.Value = TempO Or TempA Then
    11.                     If ActiveCell.Value = TempO And TempA Then
    12.                         bma = bmi
    13.                         bmd = bmi
    14.                         Marr(3) = Cell.Offset(0, bma).Value 'Sa
    15.                         Marr(7) = Cell.Offset(0, bmd).Value 'Sb
    16.                         Exit Do
    17.                     ElseIf ActiveCell.Value = TempO Then
    18.                         bmd = bmi
    19.                         Marr(7) = Cell.Offset(0, bmd).Value 'Sb
    20.                         If Not bma = 0 And bmd = 0 Then Exit Do
    21.                     ElseIf ActiveCell.Value = TempA Then
    22.                         bma = bmi
    23.                         Marr(3) = Cell.Offset(0, bma).Value 'Sa
    24.                         If Not bma = 0 And bmd = 0 Then Exit Do
    25.                     End If
    26.                 End If
    27.                     If IsEmpty(ActiveCell) Then
    28.             msg = "De ingevoerde waarde temperatuur is niet gevonden bij de boutmaterialen. Druk op ignore als u verder wilt gaan met de hoogst gevonden temperatuur."
    29.             hdr = "Temperatuur niet gevonden"
    30.             FDBCK = MsgBox(msg, vbAbortRetryIgnore Or vbInformation Or vbDefaultButton3, hdr)
    31.             Select Case FDBCK
    32.                 Case 3
    33.                     Exit Function
    34.                 Case 4
    35.                     GoTo Retry_Bmat
    36.                 Case 5
    37.                     If ITd = 0 And ITa = 0 Then
    38.                         ITd = ITc
    39.                         ITa = ITc
    40.                         TempO = ActiveCell.Offset(0, -1).Value
    41.                         TempA = ActiveCell.Offset(0, -1).Value
    42.                     ElseIf ITd = 0 Then
    43.                         ITd = ITc
    44.                         TempO = ActiveCell.Offset(0, -1).Value
    45.                     ElseIf ITa = 0 Then
    46.                         ITa = ITc
    47.                         TempA = ActiveCell.Offset(0, -1).Value
    48.                     End If
    49.                 End Select
    50.                 Exit For
    51.             End If
    52.             bmi = bmi + 1
    53.             ActiveCell.Offset(0, 1).Select
    54.             Loop
    55.             Exit For
    56.         End If
    57. Next Cell

    This is the part where things go wrong:
    vb Code:
    1. Do While Not IsEmpty(ActiveCell)
    2.                 If ActiveCell.Value = TempO Or TempA Then
    3.                     If ActiveCell.Value = TempO And TempA Then

    Even if the value of the activecell isnt equall to TempO or TempA it still proceeds as if the value was. Then if the value is equall to either TempO or TempA it is also continueing through the second "If" statement and executing the code it contains.

    Why? what am I doing wrong?

    the OR should only work if one of the is equalls are true or both of them, and the AND should only work if both of them are true... right?
    "For all men are equal before fish."
    ~ Herbert Hoover ~

  2. #2
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439

    Re: If ... AND ... then problems

    Try changing to:

    If ActiveCell.Value = TempO Or ActiveCell.Value = TempA Then....

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: If ... AND ... then problems

    Just to be clear, this sort of statement runs as follows:

    IF (evaluation to True or False) OR (evaluation to True or False) THEN ...


    so you are saying:

    IF (ActiveCell.Value = TempO) OR (TempA) THEN .....


    Now, (ActiveCell.Value = TempO) can be evaluated exactly as it is, whereas there are no evaluative criteria for (TempA). Hence Excel will just evaluate TempA to be TRUE if it is something, and FALSE if it is nothing. Hence having any value for TempA will return TRUE in the evaluation, and hence the observed behaviour.
    To correct it, you need to tell your code to perform a second evaluation, as suggested above.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2010
    Posts
    19

    Re: If ... AND ... then problems

    Aah, yeah now I see where it went wrong...

    Well Ill rewrite that part, and then it should work like a charm!

    Thanks!
    "For all men are equal before fish."
    ~ Herbert Hoover ~

Posting Permissions

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



Click Here to Expand Forum to Full Width