按“自动刷新日期和时间”;删除“;输入Excel
我使用一个简单的代码在excel工作表中的两个单独的单元格中自动输入日期和时间,但是,如果我在单元格中输入新值或只需按“删除”键,它们会自动更改。下面是我正在使用的代码:按“自动刷新日期和时间”;删除“;输入Excel,excel,date,macros,vba,Excel,Date,Macros,Vba,我使用一个简单的代码在excel工作表中的两个单独的单元格中自动输入日期和时间,但是,如果我在单元格中输入新值或只需按“删除”键,它们会自动更改。下面是我正在使用的代码: Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column <> 5 Then Exit Sub Application.EnableEvents = False Target.Offset(0, -2).Value = Date App
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 5 Then Exit Sub
Application.EnableEvents = False
Target.Offset(0, -2).Value = Date
Application.EnableEvents = True
If Target.Column <> 5 Then Exit Sub
Application.EnableEvents = False
Target.Offset(0, -1).Value = Time
Application.EnableEvents = True
End Sub
Private子工作表\u更改(ByVal目标作为范围)
如果目标为第5列,则退出子列
Application.EnableEvents=False
Target.Offset(0,-2)。值=日期
Application.EnableEvents=True
如果目标为第5列,则退出子列
Application.EnableEvents=False
Target.Offset(0,-1)。值=时间
Application.EnableEvents=True
端接头
我需要日期和时间保持静态,直到我从各自的单元格中删除它们。如何实现这一点?逐步完成您的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 5 Then Exit Sub
将此值翻转为false可确保此代码在该值设置为true之前不会再次运行<代码>工作表\u更改需要启用启用事件
。因此,现在如果更改的单元格位于列E
中,那么工作表\u Change
将不会再次执行。当通过此代码更改单元格时,避免发生无限循环是有意义的
Target.Offset(0, -2).Value = Date
将从目标单元格返回两列的单元格设置为当前日期
Application.EnableEvents = True
将启用事件设置回启用状态。这是很好的,因为你可能不想放弃这个
If Target.Column <> 5 Then Exit Sub
好的。。我们刚刚打开了,但现在我们又关闭了。别说了
Target.Offset(0, -1).Value = Time
将目标单元格左侧的值1列设置为当前时间。库里奥斯
Application.EnableEvents = True
重新打开enableEvents
。这在这里是有道理的
End Sub
重写此项以删除冗余切换和超级目标。列检查:
Private Sub Worksheet_Change(ByVal Target As Range)
'make sure this is column 5 that was changed. Like if anything changed in
' column 5, then run the rest of this.
If Target.Column <> 5 Then Exit Sub
'Make sure we don't infinite loop if we accidently trigger a change to
' column 5 in this code.
Application.EnableEvents = False
' Set two cells to the left to the current date
' and one cell to the left to the current time
Target.Offset(0, -2).Value = Date
Target.Offset(0, -1).Value = Time
'turn events back on.
Application.EnableEvents = True
End Sub
Private子工作表\u更改(ByVal目标作为范围)
'确保这是已更改的第5列。好像有什么变化
'第5列,然后运行其余部分。
如果目标为第5列,则退出子列
'如果我们意外触发对的更改,请确保我们不会无限循环
'此代码中的第5列。
Application.EnableEvents=False
'将左侧的两个单元格设置为当前日期
'左侧的一个单元格表示当前时间
Target.Offset(0,-2)。值=日期
Target.Offset(0,-1)。值=时间
“重新打开事件。
Application.EnableEvents=True
端接头
所以。。每次在第5列中进行更改时,日期和时间都会更改。如果您希望它只更改一行的日期和时间一次。然后检查是否已为行设置了日期和时间:
Private Sub Worksheet_Change(ByVal Target As Range)
'make sure this is column 5 that was changed. Like if anything changed in
' column 5, then run the rest of this.
If Target.Column <> 5 Then Exit Sub
'Check to see if the date and time are already set for this row:
' If they are, then exit subroutine.
If target.offset(0,-2).value <> "" OR target.offset(0,-1).value <> "" Then Exit Sub
'Make sure we don't infinite loop if we accidently trigger a change to
' column 5 in this code.
Application.EnableEvents = False
' Set two cells to the left to the current date
' and one cell to the left to the current time
Target.Offset(0, -2).Value = Date
Target.Offset(0, -1).Value = Time
'turn events back on.
Application.EnableEvents = True
End Sub
Private子工作表\u更改(ByVal目标作为范围)
'确保这是已更改的第5列。好像有什么变化
'第5列,然后运行其余部分。
如果目标为第5列,则退出子列
'检查是否已为此行设置了日期和时间:
'如果是,则退出子例程。
如果目标.offset(0,-2).value“”或目标.offset(0,-1).value“”,则退出Sub
'如果我们意外触发对的更改,请确保我们不会无限循环
'此代码中的第5列。
Application.EnableEvents=False
'将左侧的两个单元格设置为当前日期
'左侧的一个单元格表示当前时间
Target.Offset(0,-2)。值=日期
Target.Offset(0,-1)。值=时间
“重新打开事件。
Application.EnableEvents=True
端接头
一旦输入日期/时间,这将保留日期/时间:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 5 Then Exit Sub
Application.EnableEvents = False
If Target.Offset(0, -2).Value = "" And Target.Offset(0, -2).Value = "" Then
Target.Offset(0, -2).Value = Date
Target.Offset(0, -1).Value = Time
End If
Application.EnableEvents = True
End Sub
Private子工作表\u更改(ByVal目标作为范围)
如果目标为第5列,则退出子列
Application.EnableEvents=False
如果Target.Offset(0,-2).Value=“”和Target.Offset(0,-2).Value=“”,则
Target.Offset(0,-2)。值=日期
Target.Offset(0,-1)。值=时间
如果结束
Application.EnableEvents=True
端接头
编辑#1:
此版本将允许您设置和清除列E中的多个单元格:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, i1 As Long, i2 As Long
If Target.Column <> 5 Then Exit Sub
With ActiveSheet.UsedRange
i2 = .Rows.Count + .Row - 1
i1 = .Row
End With
Application.EnableEvents = False
For Each r In Intersect(Target, Range("E" & i1 & ":E" & i2))
If r.Offset(0, -2).Value = "" And r.Offset(0, -1).Value = "" And r.Value <> "" Then
r.Offset(0, -2).Value = Date
r.Offset(0, -1).Value = Time
End If
Next r
Application.EnableEvents = True
End Sub
Private子工作表\u更改(ByVal目标作为范围)
变暗r为范围,i1为长度,i2为长度
如果目标为第5列,则退出子列
使用ActiveSheet.UsedRange
i2=.Rows.Count+.Row-1
i1=.Row
以
Application.EnableEvents=False
对于相交中的每个r(目标、范围(“E”&i1&“:E”&i2))
如果r.Offset(0,-2).Value=“”和r.Offset(0,-1).Value=“”和r.Value”“,则
r、 偏移量(0,-2)。值=日期
r、 偏移量(0,-1)。值=时间
如果结束
下一个r
Application.EnableEvents=True
端接头
清除已空的单元格不会导致时间/日期记录。暂时效果很好。我正在桌子上工作。在我按下“删除”键后,日期和时间自动出现在所需的单元格中。但后来我选择了E列中的所有单元格,按下“Delete”(删除),弹出一个错误,我对此不予理会,现在代码根本不起作用。我看到光标显示刷新操作,但没有显示日期和时间。每次我在E列中选择多个单元格后打开文件并按“Delete”时都会发生这种情况。“它会引发运行时错误“13”:类型不匹配”@SameerGupta对于多个单元格,我们需要一个循环。我将更新代码。@SameerGupta请看我的编辑#1。太好了!很有魅力
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 5 Then Exit Sub
Application.EnableEvents = False
If Target.Offset(0, -2).Value = "" And Target.Offset(0, -2).Value = "" Then
Target.Offset(0, -2).Value = Date
Target.Offset(0, -1).Value = Time
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, i1 As Long, i2 As Long
If Target.Column <> 5 Then Exit Sub
With ActiveSheet.UsedRange
i2 = .Rows.Count + .Row - 1
i1 = .Row
End With
Application.EnableEvents = False
For Each r In Intersect(Target, Range("E" & i1 & ":E" & i2))
If r.Offset(0, -2).Value = "" And r.Offset(0, -1).Value = "" And r.Value <> "" Then
r.Offset(0, -2).Value = Date
r.Offset(0, -1).Value = Time
End If
Next r
Application.EnableEvents = True
End Sub