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
excel vba excel-vba
add a comment |
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
excel vba excel-vba
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
add a comment |
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
excel vba excel-vba
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
excel vba excel-vba
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
add a comment |
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
add a comment |
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
add a comment |
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
You could also-(criteria1 > 0) + -(...
– Alex K.
Nov 8 at 17:37
add a comment |
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
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
add a comment |
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
add a comment |
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
add a comment |
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
(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
edited Nov 8 at 16:20
answered Nov 8 at 16:10
cybernetic.nomad
1,8681515
1,8681515
add a comment |
add a comment |
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
You could also-(criteria1 > 0) + -(...
– Alex K.
Nov 8 at 17:37
add a comment |
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
You could also-(criteria1 > 0) + -(...
– Alex K.
Nov 8 at 17:37
add a comment |
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
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
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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