添加“现在”,如果有东西添加到单元格

当单元格为空时,我不希望这个代码执行。 当我尝试删除单元格的内容时,此代码将添加Now

 Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) If Target.Column = 1 Then Range("E" & Target.Row) = Now() End If End Sub 

这是你正在尝试?

 Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo Whoa If Target.Cells.CountLarge > 1 Then Exit Sub Application.EnableEvents = False If Not Intersect(Target, Columns(1)) Is Nothing Then If Target.Value <> "" Then Target.Offset(, 4).Value = Now End If Letscontinue: Application.EnableEvents = True Exit Sub Whoa: MsgBox Err.Description Resume Letscontinue End Sub 

关于Worksheet_Change更多解释可以在这里find

编辑

我试过testing没有工作! date没有显示不知道哪里错了! – 霍比6个小时前

上面的代码是针对一个工作表的,并且应该粘贴在相关的表单代码区域中。 如果要使其适用于所有工作表,请使用以下代码并将其粘贴到ThisWorkbook代码区中

 Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) On Error GoTo Whoa If Target.Cells.CountLarge > 1 Then Exit Sub Application.EnableEvents = False If Not Intersect(Target, Columns(1)) Is Nothing Then If Target.Value <> "" Then Target.Offset(, 4).Value = Now End If Letscontinue: Application.EnableEvents = True Exit Sub Whoa: MsgBox Err.Description Resume Letscontinue End Sub 

为了防止你的代码在目标是空的时候执行,只需要做一些小改动:

 Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) If Target.Column = 1 And Target.Count = 1 Then If Len(Target) Then Application.EnableEvents = False Range("E" & Target.Row) = Now() Application.EnableEvents = True End If End If End Sub