Question : Need help with a time value format issue.

I have a vba script that processes some raw data for a report.
One of the columns in the raw data contains the date and time in the following format.
yyyy-mm-yy:hh-mm-ss (24hour clock)
an example: 2008-11-11:00-00-00

The data is collected and stamped every 15mins so the time values look like
00-00-00
00-15-00
00-30-00
00-45-00
01-00-00
01-15-00
etc.

My code breaks apart the date and time, putting them into seperate columns.
Then I change the seperator character in the time value from - to : making the value hh:mm:ss
I have the format of the time col set to "hh:mm AM/PM;@"

The problem I am having shows up when a time value could equal a date value...
Example
If the time is 00-00-00 then it is formated as 12:00 AM (no possible way it could be a date)
but if the time is say 01-15-00 which could be date of 1/15/2000, then the time value is being set as
1/15/2000  12:00:00 AM (only the 12:00AM part shows in the cell, the rest shows in the function bar).
Any ideas on how to fix this in a simple manner?
I am sure I can add some code that will break apart the time value, test for AM/PM then write the date as a text value. But I think there has to be an easier way.

Answer : Need help with a time value format issue.

Okay this will do what you are looking for...basis of your raw-data that you uploaded...

Saurabh...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
Sub convert()
Dim rng As Range, cell As Range, x As String
 
Set rng = Range("K2:K625")
For Each cell In rng
If (cell.Value <> "") Then
cell.NumberFormat = "h:mm:ss"
cell.Value = TimeValue((Left(cell.Value, 2) & ":" & Mid(cell.Value, 4, 2) & ":" & Right(cell.Value, 2)))
cell.Formula = "=TEXT(" & cell.Value & ",""HH:MM AM/PM"")"
cell.Copy
cell.PasteSpecial xlPasteValues
Application.CutCopyMode = False
 
End If
Next cell
 
End Sub
Open in New Window Select All
Random Solutions  
 
programming4us programming4us