VBA code error

Occasional Contributor

Hi everyone I have this code here it works fine but if I hit "clear content" on the cell the code ends and no longer works, any idea or tips on how to fix?

this code converts hhmm to hh:mm am/pm.

Thank you.




Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("a:a")) Is Nothing Then Exit Sub
Dim xHour As String
Dim xMinute As String
Dim xWord As String
Application.EnableEvents = False
xWord = Format(Target.Value, "0000")
xHour = Left(xWord, 2)
xMinute = Right(xWord, 2)
On Error Resume Next
Target.Value = TimeValue(xHour & ":" & xMinute)
On Error Resume Next
Application.EnableEvents = True
End Sub




1 Reply


See my latest reply in Excel Time Formatting