我们如何找到列中输入的最后一项

时间:2015-08-15 06:46:36

标签: excel

我们如何找到列中输入的最后一项?(请注意,最后输入的项目可能是A4,而我们的数据是A1000)

由于

2 个答案:

答案 0 :(得分:0)

我会创建一个帮助列。这将是使用VBA生成的日期戳。您可以隐藏我们只是将其称为B列。

这将在工作表更改事件

下进行
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 1 Then
        Application.EnableEvents = False
        Me.Cells(Target.Row, 2) = Format(Date + Time, "mm/dd/yyyy h:nn:ss")
        Application.EnableEvents = True
    End If End Sub

请注意,在Me.Cells(Target.Row,2)中,2将根据您希望日期所在的列进行更改。

这将在一个单独的模块中进行:

Sub get_LastEntered()
    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim last_Time As Date
    Dim last_Row As Long
    Dim last_Row_Changed As Long

    last_Row = ws.Cells(Rows.Count, 2).End(xlUp).Row
    last_Time = Application.WorksheetFunction.Max(ws.Range("B1:B" & last_Row))

    last_Row_Changed = Application.WorksheetFunction.Match(last_Time, ws.Range("B1:B" & last_Row),0)

    MsgBox "The last Cell that you changed was:" & last_Row_Changed

End Sub

答案 1 :(得分:0)

如果您需要输入的最后一项的,请在工作表代码区中包含此事件宏:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim CellsToWatch As Range, LastValue As Range
    Set CellsToWatch = Range("A1:A1000")
    Set LastValue = Range("B1")

    If Target.Count > 1 Then Exit Sub
    If Intersect(Target, CellsToWatch) Is Nothing Then Exit Sub

    Application.EnableEvents = False
        LastValue.Value = Target.Value
    Application.EnableEvents = True
End Sub

如果您需要输入的最后一项的位置,请使用此项:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim CellsToWatch As Range, LastValue As Range
    Set CellsToWatch = Range("A1:A1000")
    Set LastValue = Range("B1")

    If Target.Count > 1 Then Exit Sub
    If Intersect(Target, CellsToWatch) Is Nothing Then Exit Sub

    Application.EnableEvents = False
        LastValue.Value = Target.Address
    Application.EnableEvents = True
End Sub

结果将存储在单元格 B1

相关问题