【问题标题】:timestamp once all cells are filled in填写所有单元格后的时间戳
【发布时间】:2013-12-03 13:27:53
【问题描述】:

情况是这样的

AG 列被设计为下拉列表(数据验证):姓名、编号、ID、电话等。到达办公室后,每个员工必须在行的每个单元格中填写他们的信息,在 A 到 G 列中。

我想从 VBA 代码中得到什么:

只有当每个单元格都填写A:G时,日期和时间才会在相应的单元格中,在H列中标记。它是永久的。它永远不会改变。一旦盖上日期,单元格列A:G 也会被锁定。

到目前为止我的编码:

Private Sub Worksheet_Change(ByVal Target As Range)
     If Target.Column = 1 Then
          Target.Offset(0,1) = Now
     End If
End Sub

此时间戳仅在 A 列中的单元格发生更改时有效:(

我应该使用“案例选择”语句吗?

【问题讨论】:

    标签: excel timestamp vba


    【解决方案1】:

    这是你正在尝试的吗? (经过试验和测试)

    Option Explicit
    
    '~~> Change this to the relevant password
    Const MYPASSWORD As String = "BlahBlah"
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Cells.CountLarge > 1 Then Exit Sub
    
        On Error GoTo Whoa
    
        Application.EnableEvents = False
    
        Dim rng As Range
        Dim nRow As Long
    
        nRow = Target.Row
    
        Set rng = Range("A" & nRow & ":G" & nRow)
    
        '~~> Check if all cell from A-G are filled and
        '~~> There is no time stamp already there
        If Application.WorksheetFunction.CountA(rng) = 7 _
        And Len(Trim(Range("H" & nRow).Value)) = 0 Then
           ActiveSheet.Unprotect MYPASSWORD
           Range("H" & nRow).Value = Now
           Range("A" & nRow & ":H" & nRow).Locked = True
           ActiveSheet.Protect MYPASSWORD
        End If
    Letscontinue:
        Application.EnableEvents = True
        Exit Sub
    Whoa:
        MsgBox Err.Description
        Resume Letscontinue
    End Sub
    

    【讨论】:

    • 您可能还想查看THIS LINK 以了解如何使用Worksheet_Change
    • 问题:运行代码时 excel 崩溃。此外,当我更改特定行的任何单元格的值时,时间和日期也会发生变化。例如,如果我填写所有单元格,日期和时间就会被标记,太好了。但是,如果我决定更改其中一个单元格的值,则会再次标记最近的日期和时间。我如何才能使第一个条目的时间戳永久化?
    • 您使用的是哪个 Excel 版本?我可以看看你的示例文件吗?
    • 好的,excel 2007,如何上传文件?
    • 您可以将其上传到 wikisend.com 并在此处分享链接
    猜你喜欢
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多