簡體   English   中英

Excel:如何查找最后連續的列值?

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

我有一個共享的excel工作表,一直在輸入記錄。 我想查找特定名稱的最后一個連續條目(在此示例中為“ A”),並在上次出現的開始和結束時記錄該值。

附加的excel的輸出應為

  1. A,2,34 ---當我打開時有5個條目
  2. A,5,null-當我打開時有9個條目
  3. A,9,6 ---當我打開時有11個條目
  4. A,9,3 ---當我打開時有12個條目

請幫助我提供可以在同一Excel的其他標簽中使用的公式。

謝謝

excel的形象

這應該工作。

在C列中使用此公式。 從第2行開始向下運行。 row1應該無關緊要(此時沒有連續的條目)。

= IF(B1 = B2,B2& “ ”A1&“, ”&A2,“”)

您也可以讓公式顯示該值的最后一個條目。 這是針對值“ A”的。

= LOOKUP(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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM