简体   繁体   English

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

[英]How can we find the last item entered within a column

How can we find the last item entered within a column?(note that the last entered item may be A4, while we have data till A1000) 我们如何才能找到列中最后输入的项目?(请注意,最后输入的项目可能是A4,而我们拥有的数据直到A1000)

Thanks 谢谢

I would create a helper column. 我将创建一个帮助器列。 This would be a date stamp that is generated using VBA. 这将是使用VBA生成的日期戳。 You can hide we'll just call it column B. 您可以隐藏,我们将其称为B列。

this will go under worksheet change event 这将进入工作表更改事件

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

Please note that in Me.Cells(Target.Row,2) the 2 is going to change according to which column you want your date in. 请注意,在Me.Cells(Target.Row,2) ,2会根据您想要输入日期的列而改变。

this will go in a separate Module: 这将放在一个单独的模块中:

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

If you need the value of the last item entered, then include this event macro in the worksheet code area: 如果您需要输入的最后一项的 ,则在工作表代码区域中包含此事件宏:

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

If you need the location of the last item entered, then use this: 如果您需要输入最后一个项目的位置 ,请使用以下命令:

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

The result will be stored in cell B1 结果将存储在单元格B1中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM