PDA

View Full Version : Solved: formatting IF's



kroz
10-28-2010, 02:31 AM
is there an easier way of writing if's?

I have 3 conditions with values on the same row so i'm using a "for each ".

This is my "if" problem

if a and b and (c is between Vmin and V max) then do stuff
else
if a then do stuff
end if
if b then do stuff
end if
if (c is between Vmin and V max) then do stuff
end if
end if

The a, b and c values refer to the same row but different columns so i'm using cells(oneCell.row,x).value

I want to make it easier to go through

Bob Phillips
10-28-2010, 02:46 AM
If both a and b can be true, as your example suggests, I don't see any easier way. Select Case is sometimes neater, but that stops after a condition is met.

mikerickson
10-28-2010, 07:59 AM
If a,b and c are boolean values

In the OP you have four routines that you might want to call
Rt1 if all are true
Rtn2 if Not all, but a
Rtn3 if notAll, but b
Rtn4 if not all but c

with the proviso that if 1 is run, 2,3,4 will NOT, but any pair of 2,3 & 4 might be called


Select Case 4*CLng(a)+2*CLng(b)+Clng(c)
Case -7
MsgBox "all true"
Call Rtn1
Case -6
MsgBox "a and b"
Call Rtn2
Call Rtn3
Case -5
MsgBox "a and c"
Call Rtn2
Call Rtn4
Case -4
msgBox "only a"
Call Rtn2
Case -3
MsgBox "b and c"
Call Rtn3
Call Rtn4
Case - 2
MsgBox "only b"
Call Rtn3
Case -1
MsgBox "only a"
Call Rtn4
Case 0
msgBox "none"
End SelectAs XLD said, this isn't more efficient, but it seems clearer to me. If the OP code is clearer to you, use it.

kroz
10-28-2010, 10:14 PM
hm..looks clearer but more complicated, and i want the code to be clear to an untrained eye so that, if needed, it can be updated in the future.
Thanx for the help guys

Paul_Hossler
10-29-2010, 06:06 AM
Going with XLD's comment, but also indenting and 'blocking' logical blocks of code seems to meet the OP's desire to keep it maintainable


If a And b And (Vmin <= c And c <= Vmax) Then
Call dostuff1

Else
If a Then
Call dostuff2
End If

If b Then
Call dostuff3
End If

If (Vmin <= c And c <= Vmax) Then
Call dostuff4
End If

End If


Paul