LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 78
Default Time Format on Worksheet Change

Hi,

With some help today (thanks a lot Paul Robinson) I have used some code on a
Worksheet Change to
ensure that time is being entered into my spreadsheet in the correct format.
So far it all works brilliantly and all time is displayed as hh:mm no matter
what is entered whether it be a full 1030 or just 9 etc. The only problem I
have is that if the user chooses to put the : in themselves (which some will)
it returns a 'Enter A Valid Time' error. Paul helped add the Replace
statement to try and take the : out of play but it still isnt working. Could
anyone else offer any suggestions?

Basically if you enter 09:30 you get the error message but then 09:30
remains in the cell in the correct format. I could leave it like this but
when it goes to the end users I will get hundreds of phone calls because they
are getting an error even though the end result is still correct. I could
just use On Error Resume Next but I think this would be a little lazy.

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


'On Error GoTo EndMacro
If Application.Intersect(Target, Range("F8:F51")) 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
TimeStr = .Value
TimeStr = CStr(Replace(TimeStr, ":", ""))

If .HasFormula = False Then
Select Case Len(TimeStr)
Case 1 ' e.g., 1 = 01:00 AM
TimeStr = Left(TimeStr, 2) & ":00"
Case 2 ' e.g., 12 = 12:00 AM
TimeStr = TimeStr & ":00"
Case 3 ' e.g., 123 = 1:23 AM
TimeStr = Left(TimeStr, 1) & ":" & _
Right(TimeStr, 2)
Case 4 ' e.g., 1234 = 12:34 AM
TimeStr = Left(TimeStr, 2) & ":" & _
Right(TimeStr, 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




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Time format via Worksheet Change Pyrite Excel Programming 5 September 9th 08 11:47 AM
change text format to time Hans Excel Worksheet Functions 3 October 18th 07 01:26 PM
Change time format from :0 to 0:0 Donald B[_2_] Excel Worksheet Functions 4 July 6th 07 03:53 AM
How to change to time format? SF Excel Programming 2 September 4th 06 11:51 AM
change format for time value Qaspec Excel Discussion (Misc queries) 1 May 23rd 05 10:25 PM


All times are GMT +1. The time now is 02:27 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"