Say that I have 3 variables (condition1, condition2 and condition3) conditions...thus, 3 Factorial + 2 would get a total of 8 combinations.
For easy understanding, say that each condition value can either be 'Yes' or 'No' (or you can think of it as either 0 or non zero).
Condition 1 Condition 2 Condition 3 Desired Result Yes No No Condition 1 ONLY Yes Yes No Condition 1 & Condition 2 Yes No Yes Condition 1 & Condition 3 No Yes No Condition 2 ONLY No Yes Yes Condition 2 & Condition 3 No No Yes Condition 3 ONLY No No No No Condition Yes Yes Yes All Condition
My question is, how do I do in VBA to get which condition to get that result?
For this example, it is only 3 conditions, but the actual, it can be 4 to 6 conditions and thus it is not feasible to think of the many combinations it can get and to write an IF statement for each conditions.
Thus, is there an easier way?
NOTE: Macro works as I wanted. However, if I were to add more 'Sheets', that means that there's more conditions. It is very easy and quick to add more variables. But the difficult part is the IF statement where I need to capture all conditions. Thus, instead of me writing down what each message box will write based on each scenario, I need VBA to know and tell me instead.
![]()
Sub test() Dim Sheet1Range As Range, Sheet2Range As Range, Sheet3Range As Range, rng As Range Dim condition1 As String, condition2 As String, condition3 As String 'I can set these conditions depending on how many 'Sheets' I need to check Set Sheet1Range = Sheet1.Range("A1:B1") Set Sheet2Range = Sheet2.Range("A1:E1") Set Sheet3Range = Sheet3.Range("A1:D1") For Each rng In Sheet1Range If rng.Value <> 0 Then condition1 = "Yes" Exit For End If Next For Each rng In Sheet2Range If rng.Value <> 0 Then condition2 = "Yes" Exit For End If Next For Each rng In Sheet3Range If rng.Value <> 0 Then condition3 = "Yes" Exit For End If Next 'This is where I get stuck with where I need to know the easier where to loop through conditions If condition1 <> "Yes" And condition2 <> "Yes" And condition3 <> "Yes" Then MsgBox "No Condition", vbOKOnly ElseIf condition1 = "Yes" And condition2 <> "Yes" And condition3 <> "Yes" Then MsgBox "Condition 1 ONLY", vbOKOnly ElseIf condition1 <> "Yes" And condition2 = "Yes" And condition3 <> "Yes" Then MsgBox "Condition 2 ONLY", vbOKOnly ElseIf condition1 <> "Yes" And condition2 <> "Yes" And condition3 = "Yes" Then MsgBox "Condition 3 ONLY", vbOKOnly ElseIf condition1 = "Yes" And condition2 = "Yes" And condition3 <> "Yes" Then MsgBox "Condition 1 and Condition 2", vbOKOnly ElseIf condition1 = "Yes" And condition2 <> "Yes" And condition3 = "Yes" Then MsgBox "Condition 1 and Condition 3", vbOKOnly ElseIf condition1 <> "Yes" And condition2 = "Yes" And condition3 = "Yes" Then MsgBox "Condition 2 and Condition 3", vbOKOnly Else MsgBox "All Condition", vbOKOnly End If End Sub
Bookmarks