Consulting

Results 1 to 7 of 7

Thread: calling subs from conditional statements

  1. #1

    Unhappy calling subs from conditional statements

    is there a more elegant way of writing this?

    [vba]If chkBOM = False And chkpBOM = True And chkPO = True Then
    Call getpBOM
    Call getPO

    ElseIf chkBOM = False And chkpBOM = True And chkPO = False Then
    Call getpBOM

    ElseIf chkBOM = False And chkPO = True And chkpBOM = False Then
    Call getPO

    ElseIf chkBOM = True And chkPO = False And chkpBOM = False Then
    Call getBOM

    ElseIf chkBOM = True And chkPO = True And chkpBOM = False Then
    Call getBOM
    Call getPO

    ElseIf chkBOM = True And chkPO = False And chkpBOM = True Then
    Call getBOM
    Call getpBOM

    ElseIf chkBOM = True And chkPO = True And chkpBOM = True Then
    Call getBOM
    Call getpBOM
    Call getPO

    End If[/vba]

  2. #2
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Does it matter what order they're run in? Perhaps:

    [VBA]If chkBom Then getBom
    If chkpBom Then getpBom
    If chkPo Then getPo[/VBA]

    Richard

  3. #3
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Try...
    [VBA]
    If chkpBOM And chkPO And Not chkBOM Then
    Call getpBOM
    Call getPO
    ElseIf chkpBOM And Not (chkBOM Or chkPO) Then
    Call getpBOM
    ElseIf chkPO And Not (chkBOM Or chkpBOM) Then
    Call getPO
    ElseIf chkBOM And Not (chkPO Or chkpBOM) Then
    Call getBOM
    ElseIf chkBOM And chkpBOM And Not chkPO Then
    Call getBOM
    Call getpBOM
    ElseIf chkBOM And chkPO And chkpBOM Then
    Call getBOM
    Call getpBOM
    Call getPO
    End If
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Sorry, I missed one case...
    [VBA]
    If chkpBOM And chkPO And Not chkBOM Then
    Call getpBOM
    Call getPO
    ElseIf chkpBOM And Not (chkBOM Or chkPO) Then
    Call getpBOM
    ElseIf chkPO And Not (chkBOM Or chkpBOM) Then
    Call getPO
    ElseIf chkBOM And Not (chkPO Or chkpBOM) Then
    Call getBOM
    ElseIf chkBOM And chkPO And Not chkpBOM Then
    Call getBOM
    Call getPO
    ElseIf chkBOM And chkpBOM And Not chkPO Then
    Call getBOM
    Call getpBOM
    ElseIf chkBOM And chkPO And chkpBOM Then
    Call getBOM
    Call getpBOM
    Call getPO
    End If
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    cool guys, thanks for the help.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can also apply integer values which provide unique solutions for each outcome
    [vba]Sub SelectMacro()
    If chkBOM Then a = 1
    If chkpBOM Then b = 2
    If chkPO Then c = 4
    test = a + b + c
    Select Case test
    Case 1
    Call getBOM
    Case 2
    Call getpBOM
    Case 3
    Call getBOM
    Call getpBOM
    Case 4
    Call getPO
    Case 5
    Call getBOM
    Call getPO
    Case 6
    Call getpBOM
    Call getPO
    Case 7
    Call getBOM
    Call getpBOM
    Call getPO
    End Select
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Hey, Malcolm, neeto idea!
    Sid

Posting Permissions

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