Using code to automate "h:mm"

  • Thread starter Thread starter hol
  • Start date Start date
H

hol

I am using a vb code that when entering hours and minutes it adds colon
between the hours and minutes. ie if I enter 105 it gives 1:05.
The code is working find for some cells, but every now and then it throws a
track!

For instance the above example it gives me 2520:00 instead of 1:05.
Any ideas?????
 
This is the code I am using:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String
Application.EnableEvents = False
On Error GoTo EndMacro
If Application.Intersect(Target, Range("D4:H5000")) 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
With Target
If .HasFormula = False Then
Select Case Len(.Formula)
Case 1 'e.g., 1=00:01
TimeStr = "0:0" & .Value
Case 2 'e.g., 12=001.2
TimeStr = "00:" & Right(.Value, 2)
Case 3 'e.g., 735=73.5
TimeStr = Left(.Value, 1) & ":" & Right(.Value, 2)
Case 4 'e.g., 1234=123.4
TimeStr = Left(.Value, 2) & ":" & Right(.Value, 2)
Case 5 'e.g., 12345 = 1234.5 NOT 12:03:45
TimeStr = Left(.Value, 3) & ":" & Right(.Value, 2)
Case 6 'e.g., 123456 = 1234.5 NOT 12:03:45
TimeStr = Left(.Value, 4) & ":" & Right(.Value, 2)
Case Else
Err.Raise 0
End Select
..Value = TimeValue(TimeStr)
End If
End With
Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub
 
I am using a userform with textbox to insert times on to the worksheet. I
think the problem lies with that?
 
A few things.

You say you want hh:mm yet you hace cases of 5 & 6 digit inputs, what are
these for? In my response they are deleted

The single digit inpot is formatted incorrectly if you want 00:01
your msgbox was in the wrong place

The code accepted an input of 199 which when formatted as 1:99 gives an
invalid time so you must trap for this.

You should disable alerts for the minimum time required:-
Have a look at this

Its change to a singele cell a1 for debugging so change this back

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String
On Error GoTo EndMacro
If Application.Intersect(Target, Range("a1")) 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
With Target
If .HasFormula = False Then
Select Case Len(Target.Value)
Case 1 'e.g., 1=00:01
TimeStr = "00:0" & Target.Value
Case 2 'e.g., 12=001.2
If Target.Value > 59 Then GoTo EndMacro
TimeStr = "00:" & Right(Target.Value, 2)
Case 3 'e.g., 735=73.5
If Right(Target.Value, 2) > 59 Then GoTo EndMacro
TimeStr = Left(.Value, 1) & ":" & Right(Target.Value, 2)
Case 4 'e.g., 1234=123.4
If Right(Target.Value, 2) > 59 Then GoTo EndMacro
TimeStr = Left(.Value, 2) & ":" & Right(Target.Value, 2)
Case Else
EndMacro:
MsgBox "You did not enter a valid time"
TimeStr = "00:00"
End Select
Application.EnableEvents = False
Target.Value = TimeStr
Application.EnableEvents = True
End If
End With
Exit Sub

Mike
 
Thanks Mike,
The worksheet keeps a tally of hours. So one day I could enter 2hours and
30minutes (2:30) it could be more or less, depending on the work is done for
that day. I have a summary sheet that keeps a running total of the hours.
I have changed the way that the hours are inserted. I used to insert it
straight on the sheet, but now I have a userform which I insert the hours and
or minutes and that in turn inserts it in the worksheet.
Maybe that is the problem.....i dont know!
 
Back
Top