[RESOLVED] If ... AND ... then problems
So Ive set up a simple if else if statement with an "Or" and a "AND" operator.
vb Code:
Set TempRange = Blad2.Range(BoutMBegin, BoutMEind)
For Each Cell In TempRange
If Cell.Value = b_grade Then
Blad2.Range(BoutMBegin).Select
ActiveCell.Offset(-1, 1).Select
bma = 0
bmd = 0
bmi = 1
Do While Not IsEmpty(ActiveCell)
If ActiveCell.Value = TempO Or TempA Then
If ActiveCell.Value = TempO And TempA Then
bma = bmi
bmd = bmi
Marr(3) = Cell.Offset(0, bma).Value 'Sa
Marr(7) = Cell.Offset(0, bmd).Value 'Sb
Exit Do
ElseIf ActiveCell.Value = TempO Then
bmd = bmi
Marr(7) = Cell.Offset(0, bmd).Value 'Sb
If Not bma = 0 And bmd = 0 Then Exit Do
ElseIf ActiveCell.Value = TempA Then
bma = bmi
Marr(3) = Cell.Offset(0, bma).Value 'Sa
If Not bma = 0 And bmd = 0 Then Exit Do
End If
End If
If IsEmpty(ActiveCell) Then
msg = "De ingevoerde waarde temperatuur is niet gevonden bij de boutmaterialen. Druk op ignore als u verder wilt gaan met de hoogst gevonden temperatuur."
hdr = "Temperatuur niet gevonden"
FDBCK = MsgBox(msg, vbAbortRetryIgnore Or vbInformation Or vbDefaultButton3, hdr)
Select Case FDBCK
Case 3
Exit Function
Case 4
GoTo Retry_Bmat
Case 5
If ITd = 0 And ITa = 0 Then
ITd = ITc
ITa = ITc
TempO = ActiveCell.Offset(0, -1).Value
TempA = ActiveCell.Offset(0, -1).Value
ElseIf ITd = 0 Then
ITd = ITc
TempO = ActiveCell.Offset(0, -1).Value
ElseIf ITa = 0 Then
ITa = ITc
TempA = ActiveCell.Offset(0, -1).Value
End If
End Select
Exit For
End If
bmi = bmi + 1
ActiveCell.Offset(0, 1).Select
Loop
Exit For
End If
Next Cell
This is the part where things go wrong:
vb Code:
Do While Not IsEmpty(ActiveCell)
If ActiveCell.Value = TempO Or TempA Then
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?
Re: If ... AND ... then problems
Try changing to:
If ActiveCell.Value = TempO Or ActiveCell.Value = TempA Then....
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.
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!