【问题标题】:Retrieve Last but one row value检索最后一行的值
【发布时间】:2015-11-28 17:34:12
【问题描述】:

我需要检索 A/B 列中最后一行的值(日期中的值)

Ex1:第 3 行:最后一列是 AB。我需要第 3 行 C 列中的第 3 行 AA 列值(日期)。

示例 2:第 4 行:AS 中的最后一列:我需要第 4 行 C 列中的列 AR 值(日期)。

Ex 3:第 5 行:BC 中的最后一列:我需要第 5 行 C 列中的 BB 列值(日期)。

可以有 1 个或 2 个空白值。

如果有任何excel公式可以解决这种情况,请告诉我。

【问题讨论】:

  • 你有没有尝试过?如果是这样,请包括您尝试过的内容。看起来你的任务可以用 VBA 完成
  • 不...我不知道要使用哪个公式...我需要excel公式而不是通过VBA。
  • 看看here,看看它是否有助于解决您的问题。
  • 据我所知没有 excel 公式。您可以在 VBA 中构建自己的函数并在电子表格中调用它,就像它是 Excel 中的真实公式一样。
  • Thaanks.. 会尝试让您知道这个 VBA 是否工作正常..

标签: excel excel-formula


【解决方案1】:

通过迭代,您可以使用如下数组公式:(对于第 3 行)

=INDIRECT("R3C"&MAX(COLUMN(3:3)*(LEN(3:3)>0))-1, 0)

如果您不需要排除单元格本身...将其放在 C 列中,它将类似于:(对于第 3 行)

=INDIRECT("R3C"&MAX(COLUMN($D3:$ZZ3)*(LEN($D3:$ZZ3)>0))-1, 0)

如果您想简单地自动填写公式,请将"R3C" 替换为"R"&ROW()&"C"

这里显示的公式是数组公式,必须用Ctrl+Shift+Enter确认。

【讨论】:

    【解决方案2】:

    如果您倾向于使用 VBA,您可以尝试以下方法:

    ' In C3 type =GetSecondLastColumnValue()
    ' This function will keep on checking values on column at a time to the right
    ' If 3 continuous empty cells are found, it will assume that there are no more
    ' columns with values after that. The last known cell with value will be identified
    ' and value of column just to its left will be plugged in to column C
    Public Function GetSecondLastColumnValue()
    
        Dim Looper As Integer
        Looper = 1
    
        Const NoOfContinuousEmptyColumns As Integer = 3
    
        Dim m_Address As String, m_LastKnownColumnWithValue
        m_Address = Selection.Address
        m_LastKnownColumnWithValue = m_Address
    
        Do
            m_Address = Range(m_Address).Offset(0, 1).Address
            If Range(m_Address).Value = "" Then
                Looper = Looper + 1
                If Looper > NoOfContinuousEmptyColumns Then Exit Do
            Else
                Looper = 1
                m_LastKnownColumnWithValue = m_Address
            End If
        Loop
    
        m_LastKnownColumnWithValue = Range(m_LastKnownColumnWithValue).Offset(0, -1).Address
        GetSecondLastColumnValue = Range(m_LastKnownColumnWithValue).Value
    
    End Function
    

    例子:

        A   B   C   D   E   F
    1  abc def      ab  cd
    2  abc def      xy  zx  st
    

    在 C1 中,键入 =GetSecondLastColumnValue (),它将填充为 ab。 在 C2 中,键入相同的公式,它将填充为 zx

    这只是您可以使用 VBA 执行的操作的一个示例。请不在生产中使用此功能。它非常贪婪和缓慢。这只是一个插图。

    这个子程序要快得多:

    ' This method keeps on going to far right till it reaches the end of the columns
    ' Then it comes back to the last column with value, and hops to the column to its left, and remembers its value
    ' That value goes in column C. It loops through all rows you specify in startrow and endrow
    Sub PopulateColumnC()
        Dim StartRow As Integer, EndRow As Integer, RowLoop As Integer
        StartRow = 1
        EndRow = 3
    
        Dim m_Address As String
    
        For RowLoop = StartRow To EndRow
            Range("A" & RowLoop).Select
            m_Address = Selection.Address
            Do
                Selection.End(xlToRight).Select
                If m_Address = Selection.Address Then
                    Selection.End(xlToLeft).Select
                    Exit Do
                Else
                    m_Address = Selection.Address
                End If
            Loop
            m_Address = Range(Selection.Address).Offset(0, -1).Address
            Range("C" & RowLoop).Value = Range(m_Address).Value
        Next
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-02
      • 2013-12-13
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      相关资源
      最近更新 更多