简体   繁体   English

Excel:如何查找最后连续的列值?

[英]Excel: How to find the last consecutive column values?

I have a shared excel sheet with records being entered all the time. 我有一个共享的excel工作表,一直在输入记录。 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. 我想查找特定名称的最后一个连续条目(在此示例中为“ A”),并在上次出现的开始和结束时记录该值。

The output of the attached excel should be 附加的excel的输出应为

  1. A,2,34 ---when i open when there were 5 entries A,2,34 ---当我打开时有5个条目
  2. A,5,null ---when i opened when there were 9 entries A,5,null-当我打开时有9个条目
  3. A,9,6 ---when i opened when there were 11 entries A,9,6 ---当我打开时有11个条目
  4. A,9,3 ---when i opened when there were 12 entries A,9,3 ---当我打开时有12个条目

please help me with the formula that i can use in a different tab of same excel. 请帮助我提供可以在同一Excel的其他标签中使用的公式。

Thanks 谢谢

excel的形象

this should work. 这应该工作。

in column C use this formula. 在C列中使用此公式。 Works from row2 and down. 从第2行开始向下运行。 row1 should be irrelevant (no consecutive entries at this point). row1应该无关紧要(此时没有连续的条目)。

=IF(B1=B2,B2&","&A1&","&A2,"") = 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". 这是针对值“ A”的。

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

在此处输入图片说明

A UDF should be able to handle the relative loop. UDF应该能够处理相对循环。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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