检索最后一行但是一行

时间:2015-11-28 17:34:13

标签: excel excel-formula

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

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

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

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

可以有一个或两个空白值。

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

2 个答案:

答案 0 :(得分:0)

使用迭代,您可以使用如下的数组公式:(对于第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 + 输入确认。

答案 1 :(得分:0)

如果你倾向于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做的一个示例。 NOT 在生产中使用此功能。这是疯狂的贪婪和缓慢。这只是一个例子。

这个子程序要快得多:

' 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