PDA

View Full Version : IF any statement



The"Truth"
11-25-2008, 07:54 AM
How do I say this in VBA code.
If any of the ResultLabel1.Caption through ResultLabel10.Caption = "FAIL" then PassorFailLabel.Caption = "FAIL"

Elseif All ResultLabel1.Caption through ResultLable10.Caption = "PASS" Then PassorFailLabel.Caption = "PASS"

Thanks for any help

CreganTur
11-25-2008, 07:58 AM
If (ResultLabel1.Caption = "FAIL" Or ResultLabel2.Caption = "FAIL" Or
... ResultLabel10.Caption = "FAIL" Then
PassorFailLabel.Caption = "FAIL"
Else
PassorFailLabel.Caption = "Pass"
End If

Or, if you want to be explicit:
If (ResultLabel1.Caption = "FAIL" Or ResultLabel2.Caption = "FAIL" Or
... ResultLabel10.Caption = "FAIL" Then
PassorFailLabel.Caption = "FAIL"
ElseIf (ResultLabel1.Caption = "Pass" And ... ResultLabel10.Caption = "Pass" Then
PassorFailLabel.Caption = "Pass"
End If

Elipses added because I'm too lazy to type out all your labels :razz2:

HTH:thumb

mikerickson
11-25-2008, 08:23 AM
If this is happening on a userform.

For I = 1 to 10
PassorFailLabel.Caption = Userform.Controls("ResultLabel" & i).Caption
If PassorFailLabel.Caption = "Fail" Then Exit For
Next i

CreganTur
11-25-2008, 08:32 AM
If this is happening on a userform.

For I = 1 to 10
PassorFailLabel.Caption = Userform.Controls("ResultLabel" & i).Caption
If PassorFailLabel.Caption = "Fail" Then Exit For
Next i

Poo on you and your fancy loops :neener:

I should have thought of that- it's the most optimal approach.

p45cal
11-25-2008, 01:19 PM
pro-grammar?
'most optimal' ?

mikerickson
11-25-2008, 09:11 PM
If the names of the labels aren't as regular as the OP suggests
Dim oneControl As Variant
For Each oneControl In Array(Me.FirstLabel, Me.SecondLabel, Me.LastLabel)
PassOrFailLabel.Caption = oneControl.Caption
If PassOrFailLabel.Caption = "Fail" Then Exit For
Next oneControl