+ Reply to Thread
Results 1 to 12 of 12

Automatically give status to condition

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,116

    Automatically give status to condition

    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
    Attached Files Attached Files
    Last edited by dluhut; 09-21-2018 at 04:43 PM.

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,884

    Re: Automatically give status to condition

    How about
    Sub Conditions()
       Dim ary As Variant
       Dim r As Long, c As Long
       Dim st As String
       
       With Range("A1").CurrentRegion
          ary = .Resize(, .Columns.count + 1).value2
       End With
       For r = 2 To UBound(ary)
          For c = 1 To UBound(ary, 2) - 1
             If ary(r, c) = "Yes" Then If Len(st) = 0 Then st = ary(1, c) Else st = st & " & " & ary(1, c)
          Next c
          If Len(st) = 0 Then ary(r, UBound(ary, 2)) = "None" Else ary(r, UBound(ary, 2)) = st: st = ""
       Next r
       Range("A1").Resize(UBound(ary), UBound(ary, 2)).Value = ary
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,116

    Re: Automatically give status to condition

    Hi Fluff13,

    Thanks for your reply.

    The conditions that I had is actually a variable. Nothing is listed in the cell/range in Excel Workbook.

    so in short, those conditions that I had are literally

    For Each rng In Sheet1Range
        If Round(rng.Value, 0) <> 0 Then
            condition1 = "Yes"
            Exit For
        End If
    Next rng
    
    For Each rng In Sheet2Range
        If Round(rng.Value, 0) <> 0 Then
            condition2 = "Yes"
            Exit For
        End If
    Next rng
    
    For Each rng In Sheet3Range
        If Round(rng.Value, 0) <> 0 Then
            condition3 = "Yes"
            Exit For
        End If
    
    If condition1 <> "Yes" And condition2 <> "Yes" And condition3 <> "Yes" then
       Range("C" & x").value = "All Condition"
    ElseIf
    ...
    ...
    ...
    Else
      Range("C" & x).value = "No Condition"
    End If
    
    Next rng
    The IF condition (marked in red) is where I'd like to have the code where given the each conditions that can happen, give me the result value. And that result value is on the 1st post above
    Last edited by dluhut; 09-21-2018 at 04:15 PM.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Automatically give status to condition

    a formula like

    =100*($A1="yes")+10*($B1="yes")+($C1="yes")

    will return
    000
    001
    010
    011
    'etc

    depending on which column(s) is Yes.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,116

    Re: Automatically give status to condition

    Hi mikerickson,

    The table shown above is not values that's in the excel cells...but rather, to show for each condition value, what the desired result should show via VBA output...and those conditions are variables.

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,884

    Re: Automatically give status to condition

    In that case can you please supply a workbook, showing what you have & what you want?

  7. #7
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,116

    Re: Automatically give status to condition

    Any VBA experts that could help me out?

  8. #8
    Forum Contributor
    Join Date
    01-03-2018
    Location
    Fukuoka, Japan
    MS-Off Ver
    Excel 2013
    Posts
    123

    Re: Automatically give status to condition

    How about this one?

    Sub test()
        Dim SheetsCount As Integer
        Dim Sheet1Range As Range, Sheet2Range As Range, Sheet3Range As Range, rng As Range
        Dim Conditions As Long, i As Long, n As Long
        Dim fmt As String
        Dim bin As String, mes As String
        Dim buf
        
        SheetsCount = 3
        Set Sheet1Range = Sheet1.Range("A1:B1")
        Set Sheet2Range = Sheet2.Range("A1:E1")
        Set Sheet3Range = Sheet3.Range("A1:D1")
        
        Conditions = 0
        
        For Each rng In Sheet1Range
            If Round(rng.Value, 0) <> 0 Then
                Conditions = Conditions + 2 ^ 0
                Exit For
            End If
        Next
        
        For Each rng In Sheet2Range
            If Round(rng.Value, 0) <> 0 Then
                Conditions = Conditions + 2 ^ 1
                Exit For
            End If
        Next
        
        For Each rng In Sheet3Range
            If Round(rng.Value, 0) <> 0 Then
                Conditions = Conditions + 2 ^ 2
                Exit For
            End If
        Next
        
        For i = 1 To SheetsCount
            fmt = fmt & "0"
        Next
        
        bin = Format(WorksheetFunction.Dec2Bin(Conditions), fmt)
        mes = ""
        
        For i = 1 To SheetsCount
            If Mid(bin, i, 1) = 1 Then mes = "Condition " & SheetsCount - i + 1 & "," & mes
        Next
        
        If mes = "" Then
            MsgBox "No Condition"
        Else
            mes = Left(mes, Len(mes) - 1)
            buf = Split(mes, ",")
            If UBound(buf) = SheetsCount - 1 Then
                MsgBox "All Condition"
            ElseIf UBound(buf) = 0 Then
                MsgBox buf(0) & " ONLY"
            Else
                MsgBox Join(buf, " and ")
            End If
        End If
    End Sub

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Automatically give status to condition

    Assuming Header in A1:C1.
    
    Sub test()
        With Cells(1).CurrentRegion
            .Columns(4).Offset(1).Resize(.Rows.Count - 1).Formula = _
            "=Conditions(" & .Rows(1).Address & "," & .Rows(2).Address(0, 0) & ")"
        End With
    End Sub
    
    Function Conditions(rng1 As Range, rng2 As Range) As String
        Dim x
        x = Filter(rng2.Parent.Evaluate("if(" & rng2.Address & "=""Yes""," & _
                    rng1.Address(external:=True) & ")"), False, 0)
        If UBound(x) = -1 Then
            Conditions = "No Condition"
        ElseIf UBound(x) = 0 Then
            Conditions = x(0) & " ONLY"
        ElseIf UBound(x) = 1 Then
            Conditions = Join(x, " & ")
        Else
            Conditions = "All Condition"
        End If
    End Function

  10. #10
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,116

    Re: Automatically give status to condition

    Hi Jindon,

    Do I just copy and paste your code? Do I need any part of my existing code to go with yours?

    Because when I use yours, I have a run-time error 1004, and when I debug, below is the portion where it got highlighted.

    .Columns(4).Offset(1).Resize(.Rows.Count - 1).Formula = _
            "=Conditions(" & .Rows(1).Address & "," & .Rows(2).Address(0, 0) & ")"

  11. #11
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,116

    Re: Automatically give status to condition

    @yujin,

    Solved. Thank you and reps up to you

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Automatically give status to condition

    See...................

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Give status as unique for the names
    By Noman050 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-31-2018, 01:24 PM
  2. IF Condition statement for Final Status
    By Rushendra in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-02-2016, 04:50 AM
  3. [SOLVED] Check read-only status, and if locked - skip all code and give a message box
    By jedemeyer1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-08-2014, 01:32 AM
  4. Replies: 3
    Last Post: 09-11-2014, 04:32 PM
  5. [SOLVED] How to Set a Condition that colors a cell based on status
    By Dmarz in forum Excel General
    Replies: 4
    Last Post: 12-03-2012, 09:56 AM
  6. Replies: 4
    Last Post: 04-08-2012, 09:43 PM
  7. i want to give more than 6 condition by using if statement
    By Sushil in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2005, 11:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1