IF statements with AND or OR
I know how to do IF statements in visual basic, but how do I incorporate AND and OR statements? ie, a simple if statement is:
if UserForm.CheckBox1 = True Then
statement
End If
But I want one that says:
if UserForm.CheckBox1 = True or if UserForm1.CheckBox 2 = True Then
statement
End If
and the same again, but using AND not or
Thanks for any help given.
Re: IF statements with AND or OR
Like this:
Quote:
if UserForm.CheckBox1 = True or if UserForm1.CheckBox 2 = True Then
statement
End If
First OR then AND
VB Code:
if UserForm.CheckBox1 = True or _
UserForm1.CheckBox2 = True Then
' statement
End If
VB Code:
if UserForm.CheckBox1 = True and _
UserForm1.CheckBox2 = True Then
' statement
End If
Re: IF statements with AND or OR
Welcome to the forums red6000.
As a note, your "And" conditions need to come first before any "Or" conditions. ;)
Re: IF statements with AND or OR
Thanks for the replies...
so lets say I want to process the statement if:
UserForm1.Checkbox1.Value = True AND UserForm1.Checkbox2.Value = True
OR if just UserForm1.Checkbox3.Value = True
as opposed to if:
UserForm1.Checkbox1.Value = True
AND if UserForm1.Checkbox2.Value = True OR if UserForm1.Checkbox3.Value = True
then how would it look? Could I have examples of both please if possible.
Hope that makes sense
Thanks for the help!!
Re: IF statements with AND or OR
I think I understand your request...
VB Code:
If ((UserForm1.Checkbox1.Value = True) AND (UserForm1.Checkbox2.Value = True)) OR (UserForm1.Checkbox3.Value = True) Then
'Either both the first two are checked OR just the third one
End If
If (UserForm1.Checkbox1.Value = True) AND (UserForm1.Checkbox2.Value = True) AND (UserForm1.Checkbox3.Value = True) Then
'All three are checked
End If
Re: IF statements with AND or OR
The 2nd one was for the statement to be process if the 1st box is ticked and either of the other 2, so would it be:
VB Code:
If (UserForm1.Checkbox1.Value = True) AND ((UserForm1.Checkbox2.Value = True) OR (UserForm1.Checkbox3.Value = True)) Then
Re: IF statements with AND or OR
Correct. I wasnt too sure how you needed the grouping but you picked this up quite nicely. :thumb:
Re: IF statements with AND or OR
Thanks for the help. That's going to make my code so much easier instead of using loads of nested IFs to get the same effect!
Thanks again.