填写所有单元格后的时间戳

时间:2013-11-19 20:03:33

标签: excel excel-vba timestamp vba

情况如下

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列中的单元格更改时有效:(

我应该使用“案例选择”声明吗?

1 个答案:

答案 0 :(得分: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