N
Nevets
I got some help from someone a while ago in order to make it easier to enter
information in a spreadsheet. I wanted to make it so that if someone entered
the digits corresponding to a time without the colon between the hh and mm
values, the program would change the entry to the proper hh:mm format (i.e.
someone enters "530", and when they tab out, the entry changes to "05:30".
I've recently discovered a problem. Here is the VB routine that was
suggested:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tlen As Long, result As String
If Target.Column < 3 Or Target.Column > 4 Then Exit Sub
If Target.Count > 1 Then Exit Sub
tlen = Len(Target.Value)
If tlen = 0 Then Exit Sub
Application.EnableEvents = False
If tlen = 1 And Target.Value = 0 Then GoTo endtime
If Left(Target.Value, 1) = "-" Then
MsgBox ("Cannot have negative values")
Target.Value = ""
GoTo endtime
End If
If tlen < 3 Then
MsgBox ("Invalid Entry")
Target.Value = ""
GoTo endtime
End If
If Format(Target.Value, "hh:mm") <= "23:59" And _
Format(Target.Value, "hh:mm") > "00:00" Then GoTo endtime
If Target.Value > 2359 Then
MsgBox ("Can't enter a time past 23:59")
GoTo endtime
End If
If tlen = 3 Then
result = Left(Target.Value, 1) & ":" & Right(Target.Value, 2)
Else
result = Left(Target.Value, 2) & ":" & Right(Target.Value, 2)
End If
Target.Value = result
endtime:
Target.Value = Format(Target.Value, "hh:mm")
If Format(Target.Value, "hh:mm") = "00:00" Then
Select Case MsgBox("Did you really mean to enter " _
& vbCrLf & "a time of Midnight 00:00?" _
, vbYesNo Or vbQuestion Or vbDefaultButton1, "Query Time input")
Case vbYes
Case vbNo
Target.Value = ""
End Select
End If
Application.EnableEvents = True
End Sub
The problem is that if I enter "0030", I get the message-box: "Invalid
Entry".
Help??
information in a spreadsheet. I wanted to make it so that if someone entered
the digits corresponding to a time without the colon between the hh and mm
values, the program would change the entry to the proper hh:mm format (i.e.
someone enters "530", and when they tab out, the entry changes to "05:30".
I've recently discovered a problem. Here is the VB routine that was
suggested:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tlen As Long, result As String
If Target.Column < 3 Or Target.Column > 4 Then Exit Sub
If Target.Count > 1 Then Exit Sub
tlen = Len(Target.Value)
If tlen = 0 Then Exit Sub
Application.EnableEvents = False
If tlen = 1 And Target.Value = 0 Then GoTo endtime
If Left(Target.Value, 1) = "-" Then
MsgBox ("Cannot have negative values")
Target.Value = ""
GoTo endtime
End If
If tlen < 3 Then
MsgBox ("Invalid Entry")
Target.Value = ""
GoTo endtime
End If
If Format(Target.Value, "hh:mm") <= "23:59" And _
Format(Target.Value, "hh:mm") > "00:00" Then GoTo endtime
If Target.Value > 2359 Then
MsgBox ("Can't enter a time past 23:59")
GoTo endtime
End If
If tlen = 3 Then
result = Left(Target.Value, 1) & ":" & Right(Target.Value, 2)
Else
result = Left(Target.Value, 2) & ":" & Right(Target.Value, 2)
End If
Target.Value = result
endtime:
Target.Value = Format(Target.Value, "hh:mm")
If Format(Target.Value, "hh:mm") = "00:00" Then
Select Case MsgBox("Did you really mean to enter " _
& vbCrLf & "a time of Midnight 00:00?" _
, vbYesNo Or vbQuestion Or vbDefaultButton1, "Query Time input")
Case vbYes
Case vbNo
Target.Value = ""
End Select
End If
Application.EnableEvents = True
End Sub
The problem is that if I enter "0030", I get the message-box: "Invalid
Entry".
Help??