View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
hol hol is offline
external usenet poster
 
Posts: 12
Default Using code to automate "h:mm"

I am using a userform with textbox to insert times on to the worksheet. I
think the problem lies with that?
--
hol


"hol" wrote:

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
--
hol


"Pete_UK" wrote:

Post your code, and someone will be able to help.

Pete

On Nov 30, 10:19 am, hol wrote:
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?????

--
hol