【问题标题】:Finding previous occurence查找上一个事件
【发布时间】:2015-09-14 02:15:27
【问题描述】:

在这种情况下我需要帮助:

A 列的数百行以随机顺序填充颜色名称(白色、蓝色、绿色、黄色、红色)。我需要 B 列中的一个公式,该公式显示 A 列中上次出现该颜色的行号。

例子:

A   B
white   0 or not found
yellow  0 or not found
yellow  2
green   0 or not found
white   1
yellow  3 (note: not `2`, which is the first occurrence, `3` is the last)

【问题讨论】:

    标签: excel formula


    【解决方案1】:

    请看一下这个公式。它会起作用,但它要求您的数据从第 2 行开始

    将此公式放在单元格 B2 中:

    =IFERROR(LOOKUP(2,1/(A$1:A1=A2),ROW($1:1)),0)
    

    这不是数组公式,所以只要正常确认,用ENTER键即可。

    现在将 B2 复制到你需要的地方。

    【讨论】:

    • @Nincs 你能做到吗?
    【解决方案2】:

    您可以使用 VBA 函数。如果你没有学过 VBA,你可能只需要看看如何创建一个新模块。

    在新模块中,粘贴以下代码:

    Public Function FindColorPosition(m_Range As Range) As String
    
        FindColor = "not found"
        If m_Range.Row = 1 Then
            Exit Function
        End If
    
        Dim i As Integer
        For i = m_Range.Row - 1 To 1 Step -1
            If m_Range.Value = Range("A" & i).Value Then
                FindColor = Str(i)
                Exit Function
            End If
        Next
    
    End Function
    

    然后,在单元格 B1 中输入 =FindColorPosition(A1) 然后,在单元格 B2 中输入 =FindColorPosition(A2) 等等……

    您将获得not found 或最后一次看到该颜色的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多