I want to use the code below in Cells C15:C21. Also, I want to use a Variation of the code for G15:G21. Can someone PLEASE help me how to do that? Thank you, thank you thank you!!!
Private Sub Worksheet_Change(ByVal Target As Excel.range)
Dim TimeStr As String
On Error GoTo EndMacro
If Application.Intersect(Target, range("c15:c21")) Is Nothing Then
Exit Sub
End If
If Target.Cells.Count > 1 Then
Exit Sub
End If
If Target.Value = "" Then
Exit Sub
End If
Application.EnableEvents = False
With Target
If .HasFormula = False Then
Select Case Len(.Value)
Case 1 ' e.g., 1 = 01:00 AM
TimeStr = "0" & .Value & ":0 AM"
Case 2 ' e.g., 12 = 12:00 AM
TimeStr = .Value & ":0 AM"
Case 3 ' e.g., 735 = 7:35 AM
TimeStr = Left(.Value, 1) & ":" & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 12:34 AM
TimeStr = Left(.Value, 2) & ":" & _
Right(.Value, 2)
Case 6 ' e.g., 123 pm = 1:23 PM
HourStr = Left(.Value, 1)
MinuteTempStr = Left(.Value, 3)
MinuteStr = Right(MinuteTempStr, 2)
TimeStr = HourStr & ":" & MinuteStr & Right(.Value, 2)
Case 7 ' e.g., 1234 pm = 12:34 PM
HourStr = Left(.Value, 2)
MinuteTempStr = Left(.Value, 4)
MinuteStr = Right(MinuteTempStr, 2)
TimeStr = HourStr & ":" & MinuteStr & Right(.Value, 2)
Case Else
'Err.Raise 0
End Select
.Value = TimeValue(TimeStr)
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
'MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Excel.range)
Dim TimeStr As String
On Error GoTo EndMacro
If Application.Intersect(Target, range("c15:c21")) Is Nothing Then
Exit Sub
End If
If Target.Cells.Count > 1 Then
Exit Sub
End If
If Target.Value = "" Then
Exit Sub
End If
Application.EnableEvents = False
With Target
If .HasFormula = False Then
Select Case Len(.Value)
Case 1 ' e.g., 1 = 01:00 AM
TimeStr = "0" & .Value & ":0 AM"
Case 2 ' e.g., 12 = 12:00 AM
TimeStr = .Value & ":0 AM"
Case 3 ' e.g., 735 = 7:35 AM
TimeStr = Left(.Value, 1) & ":" & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 12:34 AM
TimeStr = Left(.Value, 2) & ":" & _
Right(.Value, 2)
Case 6 ' e.g., 123 pm = 1:23 PM
HourStr = Left(.Value, 1)
MinuteTempStr = Left(.Value, 3)
MinuteStr = Right(MinuteTempStr, 2)
TimeStr = HourStr & ":" & MinuteStr & Right(.Value, 2)
Case 7 ' e.g., 1234 pm = 12:34 PM
HourStr = Left(.Value, 2)
MinuteTempStr = Left(.Value, 4)
MinuteStr = Right(MinuteTempStr, 2)
TimeStr = HourStr & ":" & MinuteStr & Right(.Value, 2)
Case Else
'Err.Raise 0
End Select
.Value = TimeValue(TimeStr)
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
'MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub