Consulting

Results 1 to 5 of 5

Thread: Solved: formatting IF's

  1. #1
    VBAX Regular kroz's Avatar
    Joined
    Sep 2010
    Posts
    74
    Location

    Solved: formatting IF's

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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 Select
    As XLD said, this isn't more efficient, but it seems clearer to me. If the OP code is clearer to you, use it.

  4. #4
    VBAX Regular kroz's Avatar
    Joined
    Sep 2010
    Posts
    74
    Location
    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

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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

    [vba]
    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
    [/vba]

    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •