Excel: 如何找到最后一个连续的列值?

Excel: How to find the last consecutive column values?

我有一个共享的 excel sheet,记录一直在输入。我想找到特定名称的最后一个连续条目(在本例中为 'A')并记录最后一次出现的开始和结束的值。

附件excel的输出应该是

  1. A,2,34 ---当我打开时有5个条目
  2. A,5,null ---当我打开时有9个条目
  3. A,9,6 ---当我打开时有11个条目
  4. A,9,3 ---当我打开时有12个条目

请帮助我在相同 excel 的不同选项卡中使用的公式。

谢谢

这应该有效。

在 C 列中使用此公式。从 row2 开始工作。 row1 应该是无关紧要的(此时没有连续的条目)。

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

您还可以让公式显示该值的最后一个条目。这是为了值 "A".

=查找(2,1/(B:B=E1),C:C)

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