Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Help With Logical Flow

  Asked By: Jada    Date: Oct 11    Category: MS Office    Views: 577
  

I may be approaching this totally wrong so any suggetions on
different ways to approach would be greatly appreciated. Or a
different type of logical flow. I am basically trying to evaluate an
expression and if it is true, then evaluate a new expression...and if
it is false, then evaluate a different new expression?

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?

Sub Try1

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

End Sub

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Ethan Evans     Answered On: Oct 11

Does that help  you?

if Condition1 = true  then
if condition2 = true then
' do something
else (condition2 = false)
' do something else
end if
else ' (if condition 1 = false)
if condition3= true then
' do something
else (condition3 = false)
' do something else
end if
end if

 
Answer #2    Answered By: Komal Mohammad     Answered On: Oct 11

Yes. That is so great. Thank you so much. That is what is so great
about groups. Once someone explains something, it seems so simple i
wonder why i could not see it.

 
Answer #3    Answered By: Chau Tran     Answered On: Oct 11

I find flow  charts (good method) get me on track after fighting
with keyboard level algorithm design (bad method). Work on the
problem/method/algorithm in the best tool (the one which your brain
likes).. Then and only then go to the keyboard to implement (code) it
in the language of choice.

 
Answer #4    Answered By: Viheke Fischer     Answered On: Oct 11

I found that writing my work flow  in English helps me figure out the
logical flow. For example,

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
> >

Check to see if cell (1,1) and cell (2,1) contain text
If so, check to see if cell (4,1) contains text
If so, cell E2 equals 10
If not, cell E4 equals 11
If not, cell E2 = 9
if Not, Cell E4 = 11

 
Answer #5    Answered By: Jeanette Greene     Answered On: Oct 11

For my part, two things help  a lot -depends on the kind of mood I am in-:

a graphical representation (such as a flow-chart)
a discussion with myself in plain english (I mean plain french)

 
Answer #6    Answered By: Isabella Campbell     Answered On: Oct 11

I also find it useful to use PrettyCode.Print. It's a 3rd party add in that
er ... prints code. The advantage as far as this thread is concerned is that
it prints the code with nice little lines down the left bracketing the IFs,
DOs SELECTs and so on. If you don't mind the little advert at the bottom of
the page you don't even need to register... which makes it effectively free.

 
Answer #7    Answered By: Logan Bouchard     Answered 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.

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




Tagged: