If then nested condition

  • Thread starter Thread starter a
  • Start date Start date
A

a

If then

If then



End if

End if

I don't understand this nested condition can you please give advice
information of how using this nested (if) and how it works and example

Thank you
 
It might be more evident if you used indenting... Here is an example for
illustrative purposes


If Car="Ford" then
If CarColor="Blue" Then
Msgbox "You own a blue Ford"
ElseIf CarColor="Black" then
Msgbox "You own a black Ford"
End If
End if

If you only need to check for 2 distinct value then you could easily check
both values in the same if statement. You need only nest when there are a
multitude of value to check for.
 
a said:
If then

If then



End if

End if

I don't understand this nested condition can you please give advice
information of how using this nested (if) and how it works and example

Thank you

Put simply, the inner if will only be evaluated if the outer if evaluates to
True.

If Dir("c:\temp\test.txt") <> "" Then
If UserWantsToDelete Then
Kill "c:\temp\test.txt"
End If
End If

What that does is check to see if the file "c:\temp\test.txt" exists. If it
does, a variable called UserWantsToDelete is checked to see if it's True. If
it is, the file is deleted.

The inner if is no good on its own, because when the file doesn't exist, the
kill statement will give a runtime error. So we enclose it in a check for
the file's existence.

Note that in order for the kill statement to execute, both conditions must
be True. This means that it could also be written this way:

If Dir("c:\temp\test.txt") <> "" And UserWantsToDelete Then
Kill "c:\temp\test.txt"
End If

or even (this is all one line despite how it looks here) :

If Dir("c:\temp\test.txt") <> "" And UserWantsToDelete Then Kill
"c:\temp\test.txt"

But the nested If blocks break up the code and make it easier to read, in my
opinion.

Hope that helps
 
Generally if-then-else tests a condition. If Condition1 is true then perform
something1 else perform something2. Nesting just means that you perform a
second if then else as one of your conditional-perform-statements. Here's an
example which you can drop into vba (between the Private Sub
ButtonName_Click() and the End Sub) associated with a button's on_click
function on a form. The first asks the question, is it now after 11/1/08 and
gives feedback depending on the answer:

'*******START CODE**********

' tests to see if now is after 11/1/08
If Now() > #11/1/2008# Then
MsgBox "Election Day is right around the corner!"
Else
' otherwise display if it is before 11/1/08
MsgBox "Don't forget election day!"
End If

'*******END CODE**********

This second one asks the same question but tests to see if it is before
11/1, after 11/1 but before 11/4, or after 11/4.

'*******START CODE**********

' tests to see if now is after 11/1/08
If Now() > #11/1/2008# Then
' tests to see if now is before 11/4/08
If Now() < #11/4/2008# Then
'only displays if now is between the two
MsgBox "Don't forget election Day!"
Else
' otherwise display if it is after 11/1/08 and 11/4/08
MsgBox "Did you miss it?"
End If
Else
' display if it is before 11/1/08
MsgBox "We can still with this thing!"
End If

'*******END CODE**********
 
Thank you for replay and help
========================================

If Car="Ford" then
If CarColor="Blue" Then
Msgbox "You own a blue Ford"
ElseIf CarColor="Black" then
Msgbox "You own a black Ford"
End If
End if

=====================================

Is this code equal to this Code :

If Car = "Ford" And CarColor= "Blue" Then

Msgbox "You own a blue Ford"

Else

Msgbox "You own a black Ford"
end if





I want tell you (should the (2 if ) condition must be true) to run
statements
 
Yes, your code = my code (in this instance) and yes the way my code is set up
(nested if statements) both must be true to be executed.
--
Hope this helps,

Daniel Pineault
 
Actually, the other code will print "You own a black Ford" if car is equal to
"Honda".
 
Back
Top