Cell B5 uses the following VBA:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String
On Error GoTo EndMacro
If Application.Intersect(Target, Range("B5:B12,D5:D12,F5:F12,H5:H12,J5:J12,L5:L12,N5:N12,B16:B23,D16:D23,F16:F23,H16:H23,J16:J23,L16:L23,N16:N23")) 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., 0001 = 00:01 AM
TimeStr = "00:0" & .Value
Case 2 ' e.g., 0012 = 00:12 AM
TimeStr = "00:" & .Value
Case 3 ' e.g., 0735 = 7:35 AM
TimeStr = Left(.Value, 1) & ":" & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 12:34
TimeStr = Left(.Value, 2) & ":" & _
Right(.Value, 2)
Case Else
Err.Raise 0
End Select
.Value = TimeValue(TimeStr)
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
Application.EnableEvents = True
End Sub
The VBA code the represents cell B5 allows a user to type in military time without the use of a colon, and automatically converts it into a real time. For instance 0700 is converted into 07:00 as a time of day. When i type 0700 into B5, the VBA works perfectly, converting it into 07:00.
Cell AB5 points to cell B5 using: =B5
Cell AB5 (using =B5) does not display 07:00. Instead it shows "0". When 1500 is typed into cell B6, it correctly converts to 15:00; but displays "1" in cell AB6, which is pointed at cell B6 (using =B6).
I have been trying to create an electronic timesheet for our organization for some time. With all the caveats that goes into our shedules, it is taking about 5,000 formulas, but I am VERY close to a working prototype! Thank you in advance to anyone who is able to help.
Bookmarks