Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Jada Mohammad   on Oct 11 In MS Office Category.

  
Question Answered By: Logan Bouchard   on Oct 11

Just taking your second paragraph ...

I am trying to use an If Then statement  to evaluate an expression.
If the expression  is true  I would like to use a second If Then
Statement. If the first If Then is false  I would like to use an
ElseIf Then Statement. If the second If Then is false I would like
to use an Else?

. If <first if> Then
. If <second if> then
. something
. Else
. else part of second if
. End If
. ElseIf <next part of first if> then
. something
. End If

Is this what you mean? It doesn't match what you said in your first paragraph,
but does seem to match your comment after the code.

Part of this is terminology. Don't use the words "evaluate an expression" or
similar. It is an ambiguous term and can apply equally (or even more equally
:-) to the actual assignment statement that you will likely perform in the
"then" or "else" part of one of the If statements. Also, it's not an "If Then"
or "ElseIf Then", it's simply an "If" or "ElseIf" - although this doesn't create
ambiguity. So, rewriting your paragraph:

I am trying to use an If statement. If the If succeeds, I would like to use a
second If. If the second If fails, I would like
to use an Else. If the first If fails I would like to use an ElseIf.

Note that I've re-ordered the paragraph too, to associate the Else with the
second If, which is how you actually write the code.

Note that I've put full-stops at the beginnings of my "code" above, to preserve
the indenting I used.

Your code didn't have any indenting, but I hope that it was stripped off by the
e-mail and was there in your original. You need to be pedantic about indents in
all programming languages. For VBA, you need to follow the

. If xxx
. statement
. statement
. ElseIf xxx
. statement
. statement
. Else
. statement
. statement
. End If

indenting very faithfully. Going back to your original code:

. If Cells(1, 1) <> "" And Cells(2, 1) <> "" Then
. If Cells(4, 1) <> "" Then
. Range("E2").Value = 10
. ElseIf Range("E4").Value = 11 Then (How to apply this to 1st If Then?)
. Range("E8").Value = 11
. Else (How to apply this to 2nd If Then?)
. Range("E2").Value = 9
. End If
. End If

the indents show that both the ElseIf and the Else are associated with the
second If. You simply need to move the ElseIf after the second If to associate
it with the first If:

. If Cells(1, 1) <> "" And Cells(2, 1) <> "" Then
. If Cells(4, 1) <> "" Then
. Range("E2").Value = 10
. Else (How to apply this to 2nd If Then?)
. Range("E2").Value = 9
. End If
. ElseIf Range("E4").Value = 11 Then (How to apply this to 1st If Then?)
. Range("E8").Value = 11
. End If

but this immediately looks wrong, of course, so we need to fix the indents:

. If Cells(1, 1) <> "" And Cells(2, 1) <> "" Then
. If Cells(4, 1) <> "" Then
. Range("E2").Value = 10
. Else (How to apply this to 2nd If Then?)
. Range("E2").Value = 9
. End If
. ElseIf Range("E4").Value = 11 Then (How to apply this to 1st If Then?)
. Range("E8").Value = 11
. End If

and it's now clear what lives with what.

Share: 

 

This Question has 6 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Help With Logical Flow Or get search suggestion and latest updates.


Tagged: