Solved

TIme format

Posted on 2014-09-15
4
108 Views
Last Modified: 2014-09-16
I have a spreadsheet that has cel formatted [hh]:mm if the user enters a "." instead of a ":" the cell formatting is not correct how tyot i replace the period Example 6.00 should be 06:00.see the attached file.

Thanks
Book1times.xlsm
0
Comment
Question by:Svgmassive
4 Comments
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 500 total points
ID: 40324135
Hi,

Would this be suitable for you?

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

  Select Case (True)
  
      Case (Intersect([I17:N17], Target) Is Nothing)
      
      Case (Not (IsNumeric(Target)))
      
      Case (Val(Target) > 1)
          Application.EnableEvents = False
          
          If InStr(CStr(Target), ".") > 0 Then
             Target = Format(Replace(CStr(Target), ".", ":"), "HH:MM")
          Else
             Target = Format(Replace(CStr(Target) & ".00", ".", ":"), "HH:MM")
          End If
          
          Application.EnableEvents = True
      
      Case Else
      
  End Select
  
End Sub

Open in new window


Please (also) see the attached workbook.
Q-28518882.xlsm
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40324253
You could set up Data Validation on these cells to limit entry between 0 and 1 (days, implicit).  That would prevent anyone from entering 6 days as a value.  

You could further enforce this by calculating the fraction of a day that the maximum number of allowable hours is.  If 8 hours is the maximum, the data validation limit would be 0.33333.  If 12 hours, then 0.5.

See the attached example workbook with data validation added.

Regards,
-Glenn
EE-Book1times.xlsx
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 40325025
Alternatively, you can set Data Validation to only accept a Time value. With this you can set a Earliest and Latest time.

However, if you are entering a length of time (rather than a clock time) and the time entered is greater than 24 Hours, the DV won't accept it.

Thanks
Rob H
0
 

Author Closing Comment

by:Svgmassive
ID: 40325109
Wonderful exactly what i needed.Thanks
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question