I have a simple code which updates cell F
with current date and time whenever something was modified.
Tried it again on Excel 2010 and it has no issue.
I tried commenting out the .NumberFormat
line and it worked before but it no longer works now.
If Target.Row < 6 Then
Exit Sub
End If
With Me.Cells(Target.Row, "F")
.Value = CDbl(Now)
.NumberFormat = "yyyy.mm.dd hh:mm:ss"
End With
It's supposed to update the cell with current date and time but now it does nothing and makes Excel unresponsive. I need to force-close it to be able to work on the file again.
From Target
variable I guess you are handling events. But your code raises also events, which you probably handle, which results in event loop, each calling you event handler again and again. You need to turn off events for time when macro runs:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row < 6 Then
Exit Sub
End If
' turn off events
Application.EnableEvents = False
With Me.Cells(Target.Row, "F")
.Value = CDbl(Now)
.NumberFormat = "yyyy.mm.dd hh:mm:ss"
End With
Application.EnableEvents = True
End Sub
Try this one
If Selection.Row < 6 Then
Exit Sub
End If
With ActiveSheet.Cells(Selection.Row, 6)
.Value = Now
.NumberFormat = "yyyy.mm.dd hh:mm:ss"
End With
I am assuming that you are in the active sheet as you did not expose the declaration of Me and Target. If not, just custom it.
I have encountered this problems before. Try this
If Selection.Row > 6 Then
Exit Sub
End If
ActiveSheet.Cells(Selection.Row, 6) = Format(now,"yyyy.mm.dd hh:mm:ss")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.