+ Reply to Thread
Results 1 to 11 of 11

Excel Formula to find a Sequence

Hybrid View

  1. #1
    Registered User
    Join Date
    09-05-2006
    Posts
    14

    Excel Formula to find a Sequence

    I am not very good with excel but I am looking for a formula that would calculate the following

    A B
    3 1 True
    4 2 True
    3 5 False Transition
    6 7 False
    5 8 False
    6 5 No Sequence Transition
    8 9 No Sequence

    For each time A is greater than B in a sequence of 2 or more rows it says True. If A is less than B in a sequence of 2 or more rows it turns up False. When a sequence starts, it needs to read False Transition or True Transition. If there is no sequence, meaning A is greater than B in the first row, then A is less than B in the next row and then back to A greater than B in the third row, it turns up NoSequence.

    Then I need to calculate the percentage of True, False, No Sequence in the entire sample size.

    Sorry if this is confusing.

    Thanks in advance

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Excel Formula to find a Sequence

    See attached, I hope it's what you need.

    Regards,
    Antonio
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    09-05-2006
    Posts
    14

    Re: Excel Formula to find a Sequence

    Antonio,

    Thanks for the help. The count and percent look good but I cant seem to find the formula that identifies if it is a True, False, True Transition, False Transition, No Sequence or No Sequence Transition. I have thousands of lines of data I am trying to identify the sequences with a label from above (True, False, True Transition, False Transition, No Sequence, No Sequence Transition)

    Thanks

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Excel Formula to find a Sequence

    The transition rule does not apply to the very first line then?

  5. #5
    Registered User
    Join Date
    09-05-2006
    Posts
    14

    Re: Excel Formula to find a Sequence

    Correct. The transition is when the sequence is broken and it the first row of the new sequence.

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Excel Formula to find a Sequence

    Try this. A bit messy but it works for your example. No idea if it will work for other datasets, the last row part may not work.
    Sub x()
    
    Dim r As Long, b1 As Boolean, b2 As Boolean
    
    Application.ScreenUpdating = False
    
    Columns(3).ClearContents
    r = 1
    Do While Cells(r, 1) <> ""
        b1 = Cells(r, 1) > Cells(r, 2)
        b2 = Cells(r + 1, 1) > Cells(r + 1, 2)
        If b1 <> b2 Then
            Do While (Cells(r, 1) > Cells(r, 2)) <> (Cells(r + 1, 1) > Cells(r + 1, 2))
                Cells(r, 3) = "No sequence" & Cells(r, 3)
                r = r + 1
                If Cells(r + 1, 1) = "" Then
                    Cells(r, 3) = "No sequence"
                    Exit Sub
                End If
            Loop
        End If
        If b1 = b2 And b1 Then
            Do While Cells(r, 1) > Cells(r, 2)
                Cells(r, 3) = b1 & Cells(r, 3)
                r = r + 1
                If Cells(r + 1, 1) = "" Then
                    Cells(r, 3) = "True"
                    Exit Sub
                End If
            Loop
        End If
        If b1 = b2 And Not b1 Then
           Do While Cells(r, 1) < Cells(r, 2)
                Cells(r, 3) = b1 & Cells(r, 3)
                r = r + 1
                If Cells(r + 1, 1) = "" Then
                    Cells(r, 3) = "False"
                    Exit Sub
                End If
            Loop
        End If
        If Cells(r, 1) <> "" Then Cells(r, 3) = Cells(r, 3) & " Transition"
    Loop
    
    Application.ScreenUpdating = True
    
    End Sub
    EDIT: ok, I know the last row is wrong unless it is no sequence so I will look at that.

    EDIT2: have revised code above. Am sure it could be shortened but for the time being try that.
    Last edited by StephenR; 05-14-2010 at 09:36 AM.

  7. #7
    Registered User
    Join Date
    09-05-2006
    Posts
    14

    Re: Excel Formula to find a Sequence

    Thanks for the help. I tried to run the code above but Im not sure if my computer is just not powerful enough or the code was wrong. I put it in Visual Basic Editor code section and hit run. The hour glass for a mouse never went away after 1 hour. Any thoughts?

  8. #8
    Registered User
    Join Date
    09-05-2006
    Posts
    14

    Re: Excel Formula to find a Sequence

    attached file?
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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