简体   繁体   中英

Excel: How to find the last consecutive column values?

I have a shared excel sheet with records being entered all the time. I want to find the last consecutive entry of a specific Name(its 'A' in this example) and record the value at the begining and ending of last occurance.

The output of the attached excel should be

  1. A,2,34 ---when i open when there were 5 entries
  2. A,5,null ---when i opened when there were 9 entries
  3. A,9,6 ---when i opened when there were 11 entries
  4. A,9,3 ---when i opened when there were 12 entries

please help me with the formula that i can use in a different tab of same excel.

Thanks

excel的形象

this should work.

in column C use this formula. Works from row2 and down. row1 should be irrelevant (no consecutive entries at this point).

=IF(B1=B2,B2&","&A1&","&A2,"")

You can also have a formula display whatever is the last entry for that value. This is for value "A".

=LOOKUP(2,1/(B:B=E1),C:C)

在此处输入图片说明

A UDF should be able to handle the relative loop.

Option Explicit

Function LastConColVals(rng As Range, crit As String, _
                        Optional delim As String = ",")
    Dim tmp As Variant, r As Long, rr As Long

    'allow full column references
    Set rng = Intersect(rng, rng.Parent.UsedRange)

    With rng
        tmp = Array(crit, vbNullString, vbNullString)
        For r = .Rows.Count To 1 Step -1
            If .Cells(r, 2).Value = crit Then
                tmp(2) = .Cells(r, 1).Value
                For rr = r To 1 Step -1
                    If .Cells(rr, 2).Value = crit Then
                        tmp(1) = .Cells(rr, 1).Value
                    Else
                        Exit For
                    End If
                Next rr
                'option 1 - null last value for singles
                If rr = (r - 1) Then tmp(2) = "null"
                'option 2 - truncate off last value for singles
                'If rr = (r - 1) Then ReDim Preserve tmp(UBound(tmp) - 1)
                Exit For
            End If
        Next r
    End With

    LastConColVals = Join(tmp, delim)
End Function

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM