PC Review


Reply
Thread Tools Rate Thread

Correcting time input by users

 
 
Greg H.
Guest
Posts: n/a
 
      29th May 2008
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?
 
Reply With Quote
 
 
 
 
Sandy Mann
Guest
Posts: n/a
 
      29th May 2008
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

--
HTH

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

(E-Mail Removed)
Replace @mailinator.com with @tiscali.co.uk


"Greg H." <(E-Mail Removed)> wrote in message
news:6D7928DC-3841-493B-B690-(E-Mail Removed)...
>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?
>



 
Reply With Quote
 
Sandy Mann
Guest
Posts: n/a
 
      29th May 2008
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

--
HTH

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

(E-Mail Removed)
Replace @mailinator.com with @tiscali.co.uk


"Greg H." <(E-Mail Removed)> wrote in message
news:6D7928DC-3841-493B-B690-(E-Mail Removed)...
>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?
>



 
Reply With Quote
 
Greg H.
Guest
Posts: n/a
 
      29th May 2008
That is perfect. Thanks so much

"Sandy Mann" wrote:

> 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
>
> --
> HTH
>
> Sandy
> In Perth, the ancient capital of Scotland
> and the crowning place of kings
>
> (E-Mail Removed)
> Replace @mailinator.com with @tiscali.co.uk
>
>
> "Greg H." <(E-Mail Removed)> wrote in message
> news:6D7928DC-3841-493B-B690-(E-Mail Removed)...
> >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?
> >

>
>
>

 
Reply With Quote
 
Sandy Mann
Guest
Posts: n/a
 
      29th May 2008
Glad that it worked for you, thanks for the feedback.

--

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

(E-Mail Removed)
Replace @mailinator.com with @tiscali.co.uk


"Greg H." <(E-Mail Removed)> wrote in message
news:2A9A8A9D-4ABE-4A22-B5E2-(E-Mail Removed)...
> That is perfect. Thanks so much
>
> "Sandy Mann" wrote:
>
>> 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
>>
>> --
>> HTH
>>
>> Sandy
>> In Perth, the ancient capital of Scotland
>> and the crowning place of kings
>>
>> (E-Mail Removed)
>> Replace @mailinator.com with @tiscali.co.uk
>>
>>
>> "Greg H." <(E-Mail Removed)> wrote in message
>> news:6D7928DC-3841-493B-B690-(E-Mail Removed)...
>> >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?
>> >

>>
>>
>>

>



 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      29th May 2008
> 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"


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

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      29th May 2008
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


"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
>> 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"

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


 
Reply With Quote
 
Sandy Mann
Guest
Posts: n/a
 
      29th May 2008
Very good Rick. My XL97 doesn't like Replace being used like that but
something to remember when I become posh. <g>

--

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

(E-Mail Removed)
Replace @mailinator.com with @tiscali.co.uk


"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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
>
>
> "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
>>> 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"

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

>
>



 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      29th May 2008
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


"Sandy Mann" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Very good Rick. My XL97 doesn't like Replace being used like that but
> something to remember when I become posh. <g>
>
> --
>
> Sandy
> In Perth, the ancient capital of Scotland
> and the crowning place of kings
>
> (E-Mail Removed)
> Replace @mailinator.com with @tiscali.co.uk
>
>
> "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
>> 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
>>
>>
>> "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote
>> in message news:(E-Mail Removed)...
>>>> 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"
>>>
>>> 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

>>
>>

>
>


 
Reply With Quote
 
Sandy Mann
Guest
Posts: n/a
 
      29th May 2008
Yes CDate worked fine.

Thanks for the links, I will check them out.

--
HTH

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

(E-Mail Removed)
Replace @mailinator.com with @tiscali.co.uk


"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> 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
>
>
> "Sandy Mann" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Very good Rick. My XL97 doesn't like Replace being used like that but
>> something to remember when I become posh. <g>
>>
>> --
>>
>> Sandy
>> In Perth, the ancient capital of Scotland
>> and the crowning place of kings
>>
>> (E-Mail Removed)
>> Replace @mailinator.com with @tiscali.co.uk
>>
>>
>> "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote
>> in message news:(E-Mail Removed)...
>>> 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
>>>
>>>
>>> "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote
>>> in message news:(E-Mail Removed)...
>>>>> 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"
>>>>
>>>> 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
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
access: correcting time formats =?Utf-8?B?U2Vhc29u?= Microsoft Access Queries 3 20th Feb 2006 06:40 PM
Correcting start-stop time for US Holidays =?Utf-8?B?Z3JlZW5ob3Ju?= Microsoft Outlook Calendar 1 3rd Sep 2005 07:37 PM
Correcting Icon for "Active Directory Users and Computers" jchristl@zdnetmail.com Windows XP Customization 8 20th Jan 2005 04:18 PM
w32tm.exe not correcting time Moe Microsoft Windows 2000 2 17th Jul 2003 04:14 PM
w32tm.exe not correcting time Moe Microsoft Windows 2000 Setup 2 15th Jul 2003 01:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:31 PM.