Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Correcting time input by users

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg H. - 29 May 2008 18:28 GMT
I have a worksheet where employees will be entering in times.  I do
caculations based on these times and I would like a way so if the user enters
inthe time like "10:00pm" that excel will correct it to display "10:00 pm".  
Is there a way to check for this?
Sandy Mann - 29 May 2008 19:25 GMT
Would something like this do what you want?:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
   If Intersect(Target, Range("F:F")) Is Nothing Then Exit Sub
       If Target.Cells.Count > 1 Then Exit Sub

   If IsNumeric(Target) Then Exit Sub

       If Right(UCase(Target.Value), 2) = "AM" Then _
           Target.Value = Left(Target.Value, Len(Target.Value) - 2) & " AM"

       If Right(UCase(Target.Value), 2) = "PM" Then _
           Target.Value = Left(Target.Value, Len(Target.Value) - 2) & " PM"
End Sub

It also works for just 10pm

Signature

HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

sandymann2@mailinator.com
Replace @mailinator.com with @tiscali.co.uk

>I have a worksheet where employees will be entering in times.  I do
> caculations based on these times and I would like a way so if the user
> enters
> inthe time like "10:00pm" that excel will correct it to display "10:00
> pm".
> Is there a way to check for this?
Sandy Mann - 29 May 2008 19:28 GMT
Better make that:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
   If Intersect(Target, Range("F:F")) Is Nothing Then Exit Sub
       If Target.Cells.Count > 1 Then Exit Sub

   If IsNumeric(Target) Then Exit Sub

   On Error GoTo GetOut

   Application.EnableEvents = False

       If Right(UCase(Target.Value), 2) = "AM" Then _
           Target.Value = Left(Target.Value, Len(Target.Value) - 2) & " AM"

       If Right(UCase(Target.Value), 2) = "PM" Then _
           Target.Value = Left(Target.Value, Len(Target.Value) - 2) & " PM"
GetOut:
   Application.EnableEvents = True
End Sub

Signature

HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

sandymann2@mailinator.com
Replace @mailinator.com with @tiscali.co.uk

>I have a worksheet where employees will be entering in times.  I do
> caculations based on these times and I would like a way so if the user
> enters
> inthe time like "10:00pm" that excel will correct it to display "10:00
> pm".
> Is there a way to check for this?
Greg H. - 29 May 2008 19:41 GMT
That is perfect.  Thanks so much

> Better make that:
>
[quoted text clipped - 23 lines]
> > pm".
> > Is there a way to check for this?
Sandy Mann - 29 May 2008 19:50 GMT
Glad that it worked for you, thanks for the feedback.

Signature

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

sandymann2@mailinator.com
Replace @mailinator.com with @tiscali.co.uk

> That is perfect.  Thanks so much
>
[quoted text clipped - 27 lines]
>> > pm".
>> > Is there a way to check for this?
Rick Rothstein (MVP - VB) - 29 May 2008 20:36 GMT
>        If Right(UCase(Target.Value), 2) = "AM" Then _
>            Target.Value = Left(Target.Value, Len(Target.Value) - 2) & "
[quoted text clipped - 3 lines]
>            Target.Value = Left(Target.Value, Len(Target.Value) - 2) & "
> PM"

I believe you can replace the above code lines with this single line...

Target.Value = CDate(Target.Value)

and it will successfully handle 10:00p and 10:00a also. Of course, it
returns the seconds as well, but the cell can be Custom Formatted to handle
that; or we could just do it in code...

Target.Value = Replace(CDate(Target.Value), ":00 ", " ")

Rick
Rick Rothstein (MVP - VB) - 29 May 2008 20:46 GMT
Also, in thinking about it, instead of this test...

> If IsNumeric(Target) Then Exit Sub

maybe this one would be more robust...

If Not IsDate(Target.Value) Then Exit Sub

Rick

>>        If Right(UCase(Target.Value), 2) = "AM" Then _
>>            Target.Value = Left(Target.Value, Len(Target.Value) - 2) & "
[quoted text clipped - 15 lines]
>
> Rick
Sandy Mann - 29 May 2008 20:59 GMT
Very good Rick.  My XL97 doesn't like Replace being used like that but
something to remember when I become posh. <g>

Signature

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

sandymann2@mailinator.com
Replace @mailinator.com with @tiscali.co.uk

> Also, in thinking about it, instead of this test...
>
[quoted text clipped - 25 lines]
>>
>> Rick
Rick Rothstein (MVP - VB) - 29 May 2008 21:15 GMT
But the CDate worked in XL97, right?

As for the Replace function, you might be able to use one of the VB coded
functions on this page to duplicate the Replace function's capabilities...

http://www.xbeat.net/vbspeed/c_Replace.htm

You can find other VB coded functions for the other newer String functions
under the VB6 to VB5 column on this webpage...

http://www.xbeat.net/vbspeed/

Rick

> Very good Rick.  My XL97 doesn't like Replace being used like that but
> something to remember when I become posh. <g>
[quoted text clipped - 28 lines]
>>>
>>> Rick
Sandy Mann - 29 May 2008 21:21 GMT
Yes CDate worked fine.

Thanks for the links, I will check them out.

Signature

HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

sandymann2@mailinator.com
Replace @mailinator.com with @tiscali.co.uk

> But the CDate worked in XL97, right?
>
[quoted text clipped - 42 lines]
>>>>
>>>> Rick
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.