time quick entry

H

Horatio J. Bilge

I would like to do quick entry for times to the hundredth of a second (e.g.,
m:ss.00). I've used Chip Pearson's method for time quick entry as a starting
point, and edited the Cases, but it's not working for me. I've included the
code I've tried below.

Thanks for any help,
~ Horatio

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String

On Error GoTo EndMacro
If Application.Intersect(Target, Range("A1:A10")) 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 = 00:00.01 AM
TimeStr = "00:00.0" & .Value
Case 2 ' e.g., 12 = 00:00.12 AM
TimeStr = "00:00." & .Value
Case 3 ' e.g., 735 = 00:07.35 AM
TimeStr = "00:0" & Left(.Value, 1) & "." & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 00:12.34
TimeStr = "00:" & Left(.Value, 2) & "." & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23.45 NOT 12:03.45
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & "." & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34.56
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & "." & 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
 
B

Bob Phillips

Horatio,

Try this (don't forget Dave's wonderful format<g>)

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String

On Error GoTo EndMacro
If Application.Intersect(Target, Range("A1:A10")) 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 = 00:00.01 AM
TimeStr = "00:00:00.0" & .Value
Case 2 ' e.g., 12 = 00:00.12 AM
TimeStr = "00:00:00." & .Value
Case 3 ' e.g., 735 = 00:07.35 AM
TimeStr = "00:00:0" & Left(.Value, 1) & "." & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 00:12.34
TimeStr = "00:00:" & Left(.Value, 2) & "." & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23.45 NOT 12:03.45
TimeStr = "00:" & Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & "." & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34.56
TimeStr = "00:" & Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & "." & Right(.Value, 2)
Case Else
Err.Raise 0
End Select
.FormulaR1C1 = TimeStr
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub
 
H

Horatio J. Bilge

I tried this, but it still didn't work right. Any other thoughts?
~ Horatio
The result was:
1 = :01.00
12 = :12.00
123 = 1:23.00
etc.
Instead of:
1 = :00.01
12 = :00.12
123 = :01.23
1234 = :12.34
12345 = 1:23.45
123456 = 12:34.56
 
D

Dave Peterson

It looks as though you didn't use Bob's suggested code. Are you sure you put it
behind the correct worksheet?
 
B

Bob Phillips

Horatio,

That was what your code returned. mine returns

1 = :00.01
12 = :00.12
123 = :01.23
1234 = :12.34
12345 = 1:23.45
123456 = 12:34.56

jus re-tested it to make sure.

BTW, here is a slightly amended version which automatically adds Dave's
format (so good, I had to include it).

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String

On Error GoTo EndMacro
If Application.Intersect(Target, Range("A1:A10")) 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 = 00:00.01 AM
TimeStr = "00:00:00.0" & .Value
Case 2 ' e.g., 12 = 00:00.12 AM
TimeStr = "00:00:00." & .Value
Case 3 ' e.g., 735 = 00:07.35 AM
TimeStr = "00:00:0" & Left(.Value, 1) & "." & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 00:12.34
TimeStr = "00:00:" & Left(.Value, 2) & "." & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23.45 NOT 12:03.45
TimeStr = "00:" & Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & "." & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34.56
TimeStr = "00:" & Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & "." & Right(.Value, 2)
Case Else
Err.Raise 0
End Select
.FormulaR1C1 = TimeStr
.NumberFormat = "[>0.000694444444444444][m]:ss.00;\:ss.00"

End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub
 
H

Horatio J. Bilge

You're right. I had mistakenly put the code under "ThisWorkbook," and my old
code was still present on my target worksheet.
Thank you Bob for the code, and thanks, Dave, for the great format. I
appreciate your help.

~ Horatio
 
H

Horatio J. Bilge

I got my project working well (many thanks to Bob Phillips and Dave
Peterson). I've discovered a quirk, though.

I applied protection to the sheet, allowing all users to select unlocked
cells. Now when I enter a time using the quick entry method, the time is
formatted correctly, but I get the error message, "You did not enter a valid
time." When I remove the protection, it functions normally.

Any thoughts?
Thanks for your help,
~ Horatio
 
B

Bob Phillips

Horatio,

It is the setting of the numberformat in code that causes the problem.

You can either
- set the format of all of the input cells in Excel and remove that line of
code, or
- change the code to unprotect the sheet, set the format, and ;protect the
sheet again, like

Me.Unprotect
.NumberFormat = "[>0.000694444444444444][m]:ss.00;\:ss.00"
Me.Protect

I would go for the latter, but that is just my preference.
 
H

Horatio J. Bilge

I used the second option, and it works well. Is there a way to include a
password for the protection in the code?

~ Horatio

Bob Phillips said:
Horatio,

It is the setting of the numberformat in code that causes the problem.

You can either
- set the format of all of the input cells in Excel and remove that line of
code, or
- change the code to unprotect the sheet, set the format, and ;protect the
sheet again, like

Me.Unprotect
.NumberFormat = "[>0.000694444444444444][m]:ss.00;\:ss.00"
Me.Protect

I would go for the latter, but that is just my preference.

--

HTH

RP

Horatio J. Bilge said:
I got my project working well (many thanks to Bob Phillips and Dave
Peterson). I've discovered a quirk, though.

I applied protection to the sheet, allowing all users to select unlocked
cells. Now when I enter a time using the quick entry method, the time is
formatted correctly, but I get the error message, "You did not enter a valid
time." When I remove the protection, it functions normally.

Any thoughts?
Thanks for your help,
~ Horatio


included
the
 
B

Bob Phillips

Hi Horatio,

Of course there is, should have pointed that out last time

Me.Unprotect password:="Bob"
.NumberFormat = "[>0.000694444444444444][m]:ss.00;\:ss.00"
Me.Protect password:="Bob"

--

HTH

RP
(remove nothere from the email address if mailing direct)


Horatio J. Bilge said:
I used the second option, and it works well. Is there a way to include a
password for the protection in the code?

~ Horatio

Bob Phillips said:
Horatio,

It is the setting of the numberformat in code that causes the problem.

You can either
- set the format of all of the input cells in Excel and remove that line of
code, or
- change the code to unprotect the sheet, set the format, and ;protect the
sheet again, like

Me.Unprotect
.NumberFormat = "[>0.000694444444444444][m]:ss.00;\:ss.00"
Me.Protect

I would go for the latter, but that is just my preference.

--

HTH

RP

Horatio J. Bilge said:
I got my project working well (many thanks to Bob Phillips and Dave
Peterson). I've discovered a quirk, though.

I applied protection to the sheet, allowing all users to select unlocked
cells. Now when I enter a time using the quick entry method, the time is
formatted correctly, but I get the error message, "You did not enter a valid
time." When I remove the protection, it functions normally.

Any thoughts?
Thanks for your help,
~ Horatio


I would like to do quick entry for times to the hundredth of a second
(e.g.,
m:ss.00). I've used Chip Pearson's method for time quick entry as a
starting
point, and edited the Cases, but it's not working for me. I've included
the
code I've tried below.

Thanks for any help,
~ Horatio

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String

On Error GoTo EndMacro
If Application.Intersect(Target, Range("A1:A10")) 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 = 00:00.01 AM
TimeStr = "00:00.0" & .Value
Case 2 ' e.g., 12 = 00:00.12 AM
TimeStr = "00:00." & .Value
Case 3 ' e.g., 735 = 00:07.35 AM
TimeStr = "00:0" & Left(.Value, 1) & "." & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 00:12.34
TimeStr = "00:" & Left(.Value, 2) & "." & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23.45 NOT 12:03.45
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & "." & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34.56
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & "." & 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
 
H

Horatio J. Bilge

Thanks for the help.

~ Horatio


Bob Phillips said:
Hi Horatio,

Of course there is, should have pointed that out last time

Me.Unprotect password:="Bob"
.NumberFormat = "[>0.000694444444444444][m]:ss.00;\:ss.00"
Me.Protect password:="Bob"

--

HTH

RP
(remove nothere from the email address if mailing direct)


Horatio J. Bilge said:
I used the second option, and it works well. Is there a way to include a
password for the protection in the code?

~ Horatio

Bob Phillips said:
Horatio,

It is the setting of the numberformat in code that causes the problem.

You can either
- set the format of all of the input cells in Excel and remove that
line
of
code, or
- change the code to unprotect the sheet, set the format, and ;protect the
sheet again, like

Me.Unprotect
.NumberFormat = "[>0.000694444444444444][m]:ss.00;\:ss.00"
Me.Protect

I would go for the latter, but that is just my preference.

--

HTH

RP

I got my project working well (many thanks to Bob Phillips and Dave
Peterson). I've discovered a quirk, though.

I applied protection to the sheet, allowing all users to select unlocked
cells. Now when I enter a time using the quick entry method, the
time
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top