If Statments

L

Little Penny

I have a code the based on the user input of tr and rn will determine
the value of bn.
I tried to structure it as you see below but it does not pass bn on to
when it needs to be used.

Any help would be appreciated.



tr, bn and rn are declard as long


tr = InputBox("TOTAL BOXES IN THIS ORDERB.")
rn = InputBox("THIS IS BOX NUMBER?")


If tr = 4 Then

ElseIf rn = 4 Then bn = 1
ElseIf rn = 3 Then bn = 2
ElseIf rn = 2 Then bn = 3
ElseIf rn = 1 Then bn = 4



End If


If tr = 3 Then


ElseIf rn = 3 Then bn = 1
ElseIf rn = 2 Then bn = 2
ElseIf rn = 1 Then bn = 3



End If




If tr = 2 Then


ElseIf rn = 2 Then bn = 1
ElseIf rn = 1 Then bn = 2



End If




Thanks



Little Penny
 
E

eliano

Hi Little Penny.
Invalid use of ElseIf, I believe.

Try:

If tr = 4 And rn = 4 Then bn = 1
If tr = 4 And rn = 3 Then bn = 2
If tr = 4 And rn = 2 Then bn = 3
If tr = 4 And rn = 1 Then bn = 4

and so on.

Buon Natale
Eliano
 
J

JLGWhiz

Or:

If tr = 4 Then
If rn = 4 Then
bn = 1
ElseIf rn = 3 Then
bn = 2
ElseIf rn = 2 Then
bn = 3
ElseIf rn = 1 Then
bn = 4
End If
End If

When you use the ElseIf statement you have to follow the block fromat. That
means the If...then has to be on a separate line than the executable
statement and the ElseIf...Then has to be on a separate line than the
executable statment and the End If has to be on a separate line. In your
case, you wanted to use a nested If...Then...ElseIf...Then inside an
If...Then.

If...Then 'If the first condition us met
If...Then 'Then check for different condition, etc.
'do something
ElseIf...Then
'do something
End If
End If
 
D

Dave Peterson

You may want to try the select case structure:

Select case tr
case is = 4
select case rn
case is = 4:bn=1
case is = 4:bn=1
case is = 4:bn=1
 
D

Dave Peterson

Stupid fingers!

You may want to try the select case structure:

Select case tr
case is = 4
select case rn
case is = 4:bn=1
case is = 3:bn=2
case is = 2:bn=3
case is = 1:bn=4
end select
case is = 8888 '
select case rn 'do more tests here
case is = 4:bn=1
case is = 3:bn=2
case is = 2:bn=3
case is = 1:bn=4
end select
end select

======
But sometimes, you can use math:

if tr = 4 then
bn = 5 - rn
else...
 
R

Rick Rothstein

I am pretty sure that this code can replace all the code that you posted...

tr = InputBox("TOTAL BOXES IN THIS ORDERB.")
rn = InputBox("THIS IS BOX NUMBER?")
bn = 1 + tr - rn
 
Top