An if statement to run based on various amounts of criteria being greater than 0









up vote
0
down vote

favorite












I'm writing an if statement with 5 separate criteria based on whether they are greater than zero.



I want 6 separate if statements based on whether 0 criteria >0, 1 criteria > 0 , 2 etc... up to 5 criteria > 0. I was trying to do this with OR and AND statements, but it's obviously not the solution.



What I'm essentially after:



If 0 Criteria > 0 Then
Do this
ElseIf 1 Criteria > 0 Then
Do this
ElseIf 2 Criteria > 0 Then
Do this
....
ElseIf 5 Criteria > 0 Then
Do this
End If









share|improve this question



















  • 2




    So you have 5 independent numeric variables and want a different action depending on how many are 0?
    – Alex K.
    Nov 8 at 16:03






  • 1




    The criteria, are they cells on a worksheet? Are those cells contiguous?
    – Scott Craner
    Nov 8 at 16:11














up vote
0
down vote

favorite












I'm writing an if statement with 5 separate criteria based on whether they are greater than zero.



I want 6 separate if statements based on whether 0 criteria >0, 1 criteria > 0 , 2 etc... up to 5 criteria > 0. I was trying to do this with OR and AND statements, but it's obviously not the solution.



What I'm essentially after:



If 0 Criteria > 0 Then
Do this
ElseIf 1 Criteria > 0 Then
Do this
ElseIf 2 Criteria > 0 Then
Do this
....
ElseIf 5 Criteria > 0 Then
Do this
End If









share|improve this question



















  • 2




    So you have 5 independent numeric variables and want a different action depending on how many are 0?
    – Alex K.
    Nov 8 at 16:03






  • 1




    The criteria, are they cells on a worksheet? Are those cells contiguous?
    – Scott Craner
    Nov 8 at 16:11












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm writing an if statement with 5 separate criteria based on whether they are greater than zero.



I want 6 separate if statements based on whether 0 criteria >0, 1 criteria > 0 , 2 etc... up to 5 criteria > 0. I was trying to do this with OR and AND statements, but it's obviously not the solution.



What I'm essentially after:



If 0 Criteria > 0 Then
Do this
ElseIf 1 Criteria > 0 Then
Do this
ElseIf 2 Criteria > 0 Then
Do this
....
ElseIf 5 Criteria > 0 Then
Do this
End If









share|improve this question















I'm writing an if statement with 5 separate criteria based on whether they are greater than zero.



I want 6 separate if statements based on whether 0 criteria >0, 1 criteria > 0 , 2 etc... up to 5 criteria > 0. I was trying to do this with OR and AND statements, but it's obviously not the solution.



What I'm essentially after:



If 0 Criteria > 0 Then
Do this
ElseIf 1 Criteria > 0 Then
Do this
ElseIf 2 Criteria > 0 Then
Do this
....
ElseIf 5 Criteria > 0 Then
Do this
End If






excel vba excel-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 16:51









TylerH

15.3k105067




15.3k105067










asked Nov 8 at 15:57









user10451617

225




225







  • 2




    So you have 5 independent numeric variables and want a different action depending on how many are 0?
    – Alex K.
    Nov 8 at 16:03






  • 1




    The criteria, are they cells on a worksheet? Are those cells contiguous?
    – Scott Craner
    Nov 8 at 16:11












  • 2




    So you have 5 independent numeric variables and want a different action depending on how many are 0?
    – Alex K.
    Nov 8 at 16:03






  • 1




    The criteria, are they cells on a worksheet? Are those cells contiguous?
    – Scott Craner
    Nov 8 at 16:11







2




2




So you have 5 independent numeric variables and want a different action depending on how many are 0?
– Alex K.
Nov 8 at 16:03




So you have 5 independent numeric variables and want a different action depending on how many are 0?
– Alex K.
Nov 8 at 16:03




1




1




The criteria, are they cells on a worksheet? Are those cells contiguous?
– Scott Craner
Nov 8 at 16:11




The criteria, are they cells on a worksheet? Are those cells contiguous?
– Scott Craner
Nov 8 at 16:11












3 Answers
3






active

oldest

votes

















up vote
1
down vote













(this is untested)



Dim c as long

c = 0

If criteria1 = 0 then CritNo = CritNo + 1
If criteria2 = 0 then CritNo = CritNo + 1
If criteria3 = 0 then CritNo = CritNo + 1
If criteria4 = 0 then CritNo = CritNo + 1
If criteria5 = 0 then CritNo = CritNo + 1
If criteria6 = 0 then CritNo = CritNo + 1

Select case CritNo
Case 1
Do stuff
Case 2
Do other stuff
...
Case Else
Do nothing
End select





share|improve this answer





























    up vote
    1
    down vote













    Here's another idea:



    Select Case Abs(criteria1 > 0) + Abs(criteria2 > 0) + Abs(criteria3 > 0) + Abs(criteria4 > 0) + Abs(criteria5 > 0)
    Case 0
    MsgBox "0 criteria"
    Case 1
    MsgBox "1 criteria"
    Case 2
    MsgBox "2 criteria"
    Case 3
    MsgBox "3 criteria"
    Case 4
    MsgBox "4 criteria"
    Case 5
    MsgBox "5 criteria"
    End Select





    share|improve this answer




















    • You could also -(criteria1 > 0) + -(...
      – Alex K.
      Nov 8 at 17:37

















    up vote
    1
    down vote













    You could use evaluate on an array of the criteria with SUMPRODUCT



    Option Explicit
    Public Sub test()
    Dim criteria1 As Long, criteria2 As Long,criteria3 As Long
    Dim criteria4 As Long, criteria5 As Long, arr()
    Dim numGreaterThanZero As Long

    criteria1 = -1
    criteria2 = 3
    criteria3 = 0
    criteria4 = 5
    criteria5 = 6

    arr = Array(criteria1, criteria2, criteria3, criteria4, criteria5)

    numGreaterThanZero = Application.Evaluate("=SUMPRODUCT(--(" & Join$(arr, ";") & ">0))")

    Select Case numGreaterThanZero

    Case 1
    Case 2
    Case 3
    Case 4
    Case 5

    End Select
    End Sub



    If you actually just want to test the conditions separately (and not a cumulative count) and exit at first True then:



    Select Case True

    Criteria1 > 0
    'Do Something

    Criteria2> 0
    'Do something else

    End Select





    share|improve this answer






















    • Sorry I missed the then statements, silly mistake! Yes I literally thought after posting doing it top down might be the best way, so it will then bypass the next elseif statement. Thank you
      – user10451617
      Nov 8 at 16:03











    • Depends if you need to run all the tests or only need the up to the first that evaluates to True. Which is it?
      – QHarr
      Nov 8 at 16:04










    • Only up to the first one that is true
      – user10451617
      Nov 9 at 7:56










    • Was my original answer what you wanted then? Select Case statement ? I will add back in .
      – QHarr
      Nov 9 at 7:59










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53211486%2fan-if-statement-to-run-based-on-various-amounts-of-criteria-being-greater-than-0%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    (this is untested)



    Dim c as long

    c = 0

    If criteria1 = 0 then CritNo = CritNo + 1
    If criteria2 = 0 then CritNo = CritNo + 1
    If criteria3 = 0 then CritNo = CritNo + 1
    If criteria4 = 0 then CritNo = CritNo + 1
    If criteria5 = 0 then CritNo = CritNo + 1
    If criteria6 = 0 then CritNo = CritNo + 1

    Select case CritNo
    Case 1
    Do stuff
    Case 2
    Do other stuff
    ...
    Case Else
    Do nothing
    End select





    share|improve this answer


























      up vote
      1
      down vote













      (this is untested)



      Dim c as long

      c = 0

      If criteria1 = 0 then CritNo = CritNo + 1
      If criteria2 = 0 then CritNo = CritNo + 1
      If criteria3 = 0 then CritNo = CritNo + 1
      If criteria4 = 0 then CritNo = CritNo + 1
      If criteria5 = 0 then CritNo = CritNo + 1
      If criteria6 = 0 then CritNo = CritNo + 1

      Select case CritNo
      Case 1
      Do stuff
      Case 2
      Do other stuff
      ...
      Case Else
      Do nothing
      End select





      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        (this is untested)



        Dim c as long

        c = 0

        If criteria1 = 0 then CritNo = CritNo + 1
        If criteria2 = 0 then CritNo = CritNo + 1
        If criteria3 = 0 then CritNo = CritNo + 1
        If criteria4 = 0 then CritNo = CritNo + 1
        If criteria5 = 0 then CritNo = CritNo + 1
        If criteria6 = 0 then CritNo = CritNo + 1

        Select case CritNo
        Case 1
        Do stuff
        Case 2
        Do other stuff
        ...
        Case Else
        Do nothing
        End select





        share|improve this answer














        (this is untested)



        Dim c as long

        c = 0

        If criteria1 = 0 then CritNo = CritNo + 1
        If criteria2 = 0 then CritNo = CritNo + 1
        If criteria3 = 0 then CritNo = CritNo + 1
        If criteria4 = 0 then CritNo = CritNo + 1
        If criteria5 = 0 then CritNo = CritNo + 1
        If criteria6 = 0 then CritNo = CritNo + 1

        Select case CritNo
        Case 1
        Do stuff
        Case 2
        Do other stuff
        ...
        Case Else
        Do nothing
        End select






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 16:20

























        answered Nov 8 at 16:10









        cybernetic.nomad

        1,8681515




        1,8681515






















            up vote
            1
            down vote













            Here's another idea:



            Select Case Abs(criteria1 > 0) + Abs(criteria2 > 0) + Abs(criteria3 > 0) + Abs(criteria4 > 0) + Abs(criteria5 > 0)
            Case 0
            MsgBox "0 criteria"
            Case 1
            MsgBox "1 criteria"
            Case 2
            MsgBox "2 criteria"
            Case 3
            MsgBox "3 criteria"
            Case 4
            MsgBox "4 criteria"
            Case 5
            MsgBox "5 criteria"
            End Select





            share|improve this answer




















            • You could also -(criteria1 > 0) + -(...
              – Alex K.
              Nov 8 at 17:37














            up vote
            1
            down vote













            Here's another idea:



            Select Case Abs(criteria1 > 0) + Abs(criteria2 > 0) + Abs(criteria3 > 0) + Abs(criteria4 > 0) + Abs(criteria5 > 0)
            Case 0
            MsgBox "0 criteria"
            Case 1
            MsgBox "1 criteria"
            Case 2
            MsgBox "2 criteria"
            Case 3
            MsgBox "3 criteria"
            Case 4
            MsgBox "4 criteria"
            Case 5
            MsgBox "5 criteria"
            End Select





            share|improve this answer




















            • You could also -(criteria1 > 0) + -(...
              – Alex K.
              Nov 8 at 17:37












            up vote
            1
            down vote










            up vote
            1
            down vote









            Here's another idea:



            Select Case Abs(criteria1 > 0) + Abs(criteria2 > 0) + Abs(criteria3 > 0) + Abs(criteria4 > 0) + Abs(criteria5 > 0)
            Case 0
            MsgBox "0 criteria"
            Case 1
            MsgBox "1 criteria"
            Case 2
            MsgBox "2 criteria"
            Case 3
            MsgBox "3 criteria"
            Case 4
            MsgBox "4 criteria"
            Case 5
            MsgBox "5 criteria"
            End Select





            share|improve this answer












            Here's another idea:



            Select Case Abs(criteria1 > 0) + Abs(criteria2 > 0) + Abs(criteria3 > 0) + Abs(criteria4 > 0) + Abs(criteria5 > 0)
            Case 0
            MsgBox "0 criteria"
            Case 1
            MsgBox "1 criteria"
            Case 2
            MsgBox "2 criteria"
            Case 3
            MsgBox "3 criteria"
            Case 4
            MsgBox "4 criteria"
            Case 5
            MsgBox "5 criteria"
            End Select






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 16:55









            Brian M Stafford

            2,6891814




            2,6891814











            • You could also -(criteria1 > 0) + -(...
              – Alex K.
              Nov 8 at 17:37
















            • You could also -(criteria1 > 0) + -(...
              – Alex K.
              Nov 8 at 17:37















            You could also -(criteria1 > 0) + -(...
            – Alex K.
            Nov 8 at 17:37




            You could also -(criteria1 > 0) + -(...
            – Alex K.
            Nov 8 at 17:37










            up vote
            1
            down vote













            You could use evaluate on an array of the criteria with SUMPRODUCT



            Option Explicit
            Public Sub test()
            Dim criteria1 As Long, criteria2 As Long,criteria3 As Long
            Dim criteria4 As Long, criteria5 As Long, arr()
            Dim numGreaterThanZero As Long

            criteria1 = -1
            criteria2 = 3
            criteria3 = 0
            criteria4 = 5
            criteria5 = 6

            arr = Array(criteria1, criteria2, criteria3, criteria4, criteria5)

            numGreaterThanZero = Application.Evaluate("=SUMPRODUCT(--(" & Join$(arr, ";") & ">0))")

            Select Case numGreaterThanZero

            Case 1
            Case 2
            Case 3
            Case 4
            Case 5

            End Select
            End Sub



            If you actually just want to test the conditions separately (and not a cumulative count) and exit at first True then:



            Select Case True

            Criteria1 > 0
            'Do Something

            Criteria2> 0
            'Do something else

            End Select





            share|improve this answer






















            • Sorry I missed the then statements, silly mistake! Yes I literally thought after posting doing it top down might be the best way, so it will then bypass the next elseif statement. Thank you
              – user10451617
              Nov 8 at 16:03











            • Depends if you need to run all the tests or only need the up to the first that evaluates to True. Which is it?
              – QHarr
              Nov 8 at 16:04










            • Only up to the first one that is true
              – user10451617
              Nov 9 at 7:56










            • Was my original answer what you wanted then? Select Case statement ? I will add back in .
              – QHarr
              Nov 9 at 7:59














            up vote
            1
            down vote













            You could use evaluate on an array of the criteria with SUMPRODUCT



            Option Explicit
            Public Sub test()
            Dim criteria1 As Long, criteria2 As Long,criteria3 As Long
            Dim criteria4 As Long, criteria5 As Long, arr()
            Dim numGreaterThanZero As Long

            criteria1 = -1
            criteria2 = 3
            criteria3 = 0
            criteria4 = 5
            criteria5 = 6

            arr = Array(criteria1, criteria2, criteria3, criteria4, criteria5)

            numGreaterThanZero = Application.Evaluate("=SUMPRODUCT(--(" & Join$(arr, ";") & ">0))")

            Select Case numGreaterThanZero

            Case 1
            Case 2
            Case 3
            Case 4
            Case 5

            End Select
            End Sub



            If you actually just want to test the conditions separately (and not a cumulative count) and exit at first True then:



            Select Case True

            Criteria1 > 0
            'Do Something

            Criteria2> 0
            'Do something else

            End Select





            share|improve this answer






















            • Sorry I missed the then statements, silly mistake! Yes I literally thought after posting doing it top down might be the best way, so it will then bypass the next elseif statement. Thank you
              – user10451617
              Nov 8 at 16:03











            • Depends if you need to run all the tests or only need the up to the first that evaluates to True. Which is it?
              – QHarr
              Nov 8 at 16:04










            • Only up to the first one that is true
              – user10451617
              Nov 9 at 7:56










            • Was my original answer what you wanted then? Select Case statement ? I will add back in .
              – QHarr
              Nov 9 at 7:59












            up vote
            1
            down vote










            up vote
            1
            down vote









            You could use evaluate on an array of the criteria with SUMPRODUCT



            Option Explicit
            Public Sub test()
            Dim criteria1 As Long, criteria2 As Long,criteria3 As Long
            Dim criteria4 As Long, criteria5 As Long, arr()
            Dim numGreaterThanZero As Long

            criteria1 = -1
            criteria2 = 3
            criteria3 = 0
            criteria4 = 5
            criteria5 = 6

            arr = Array(criteria1, criteria2, criteria3, criteria4, criteria5)

            numGreaterThanZero = Application.Evaluate("=SUMPRODUCT(--(" & Join$(arr, ";") & ">0))")

            Select Case numGreaterThanZero

            Case 1
            Case 2
            Case 3
            Case 4
            Case 5

            End Select
            End Sub



            If you actually just want to test the conditions separately (and not a cumulative count) and exit at first True then:



            Select Case True

            Criteria1 > 0
            'Do Something

            Criteria2> 0
            'Do something else

            End Select





            share|improve this answer














            You could use evaluate on an array of the criteria with SUMPRODUCT



            Option Explicit
            Public Sub test()
            Dim criteria1 As Long, criteria2 As Long,criteria3 As Long
            Dim criteria4 As Long, criteria5 As Long, arr()
            Dim numGreaterThanZero As Long

            criteria1 = -1
            criteria2 = 3
            criteria3 = 0
            criteria4 = 5
            criteria5 = 6

            arr = Array(criteria1, criteria2, criteria3, criteria4, criteria5)

            numGreaterThanZero = Application.Evaluate("=SUMPRODUCT(--(" & Join$(arr, ";") & ">0))")

            Select Case numGreaterThanZero

            Case 1
            Case 2
            Case 3
            Case 4
            Case 5

            End Select
            End Sub



            If you actually just want to test the conditions separately (and not a cumulative count) and exit at first True then:



            Select Case True

            Criteria1 > 0
            'Do Something

            Criteria2> 0
            'Do something else

            End Select






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 8:00

























            answered Nov 8 at 16:01









            QHarr

            25.6k81839




            25.6k81839











            • Sorry I missed the then statements, silly mistake! Yes I literally thought after posting doing it top down might be the best way, so it will then bypass the next elseif statement. Thank you
              – user10451617
              Nov 8 at 16:03











            • Depends if you need to run all the tests or only need the up to the first that evaluates to True. Which is it?
              – QHarr
              Nov 8 at 16:04










            • Only up to the first one that is true
              – user10451617
              Nov 9 at 7:56










            • Was my original answer what you wanted then? Select Case statement ? I will add back in .
              – QHarr
              Nov 9 at 7:59
















            • Sorry I missed the then statements, silly mistake! Yes I literally thought after posting doing it top down might be the best way, so it will then bypass the next elseif statement. Thank you
              – user10451617
              Nov 8 at 16:03











            • Depends if you need to run all the tests or only need the up to the first that evaluates to True. Which is it?
              – QHarr
              Nov 8 at 16:04










            • Only up to the first one that is true
              – user10451617
              Nov 9 at 7:56










            • Was my original answer what you wanted then? Select Case statement ? I will add back in .
              – QHarr
              Nov 9 at 7:59















            Sorry I missed the then statements, silly mistake! Yes I literally thought after posting doing it top down might be the best way, so it will then bypass the next elseif statement. Thank you
            – user10451617
            Nov 8 at 16:03





            Sorry I missed the then statements, silly mistake! Yes I literally thought after posting doing it top down might be the best way, so it will then bypass the next elseif statement. Thank you
            – user10451617
            Nov 8 at 16:03













            Depends if you need to run all the tests or only need the up to the first that evaluates to True. Which is it?
            – QHarr
            Nov 8 at 16:04




            Depends if you need to run all the tests or only need the up to the first that evaluates to True. Which is it?
            – QHarr
            Nov 8 at 16:04












            Only up to the first one that is true
            – user10451617
            Nov 9 at 7:56




            Only up to the first one that is true
            – user10451617
            Nov 9 at 7:56












            Was my original answer what you wanted then? Select Case statement ? I will add back in .
            – QHarr
            Nov 9 at 7:59




            Was my original answer what you wanted then? Select Case statement ? I will add back in .
            – QHarr
            Nov 9 at 7:59

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53211486%2fan-if-statement-to-run-based-on-various-amounts-of-criteria-being-greater-than-0%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)