Solved

Convert numbers to time

Posted on 2013-01-18
17
485 Views
Last Modified: 2013-01-20
Guys, in excel if I type 656p  in a cell how does it convert to 6:56 PM. I don't want to have to use the colon.

Does this need to be done with vba or can it be formatted as such?

Thank you for your help,

Robert
0
Comment
Question by:rws1
  • 7
  • 7
  • 3
17 Comments
 
LVL 26

Expert Comment

by:redmondb
ID: 38795394
Robert,

You have a couple of choices...
(1) A Change Event macro which monitors the appropriate cells for any changes and converts values in the format you specify to times.
(2) Use two columns - the first is the input one, while the second is a formula that converts the input cell's value to a time.

If you want either (or both!) of these please let me know.

Regards,
Brian.
0
 

Author Comment

by:rws1
ID: 38795410
Brian, thank you for your response.

I understand how to put a formula in a 2nd cell for the conversion to time.

I am assuming from your response it must happen with vba and if this is the case could you please forward the code, that when I type 656p the result is 6:56 PM or 656a the result is 6:56 AM.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38795425
right-click on the sheet tab name and select view code and paste this code
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
If Right(Target, 1) = "p" Or Right(Target, 1) = "a" Then
Target = CVDate(Replace(Left(Target, Len(Target) - 1) / 100, ".", ":") & Right(Target, 1) & "m")
End If
End If
End Sub

Open in new window

This works for column 1. For an other column change line 2
0
 

Author Comment

by:rws1
ID: 38795444
Thank you for your response, I type your code in my editor then type 656p in column A with no success, however I need the code to work in columns d,f j, any further suggestions would be greatly appreciated.
Robert
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38795488
Try this file
Timecon.xlsm
0
 
LVL 26

Assisted Solution

by:redmondb
redmondb earned 200 total points
ID: 38795494
Robert,

Edit: Apologies, wrong range now corrected.

Please see attached. The code is...
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRange As Range
Dim xCell  As Range
Dim xlast_Row As Long
Dim xTime     As Variant
Dim xHold     As String
Dim xLast     As String
Dim xlength   As Long

xlast_Row = Me.Range("A1").SpecialCells(xlLastCell).Row
If xlast_Row < 2 Then Exit Sub

Set xRange = Intersect(Target, Me.Range("D2:D" & xlast_Row & ", F2:F" & xlast_Row & ", J2:J" & xlast_Row))

If Not xRange Is Nothing Then
    For Each xCell In xRange
        xHold = xCell
        If xHold <> "" Then
            xlength = Len(xHold)
            If xlength > 1 And xlength < 6 Then
                xLast = LCase(Mid(xHold, xlength, 1))
                If xLast = "a" Or xLast = "p" Then
                    Select Case xlength
                        Case 2
                            xHold = "00:0" & Mid(xHold, 1, 1)
                        Case 3
                            xHold = "00:" & Mid(xHold, 1, 2)
                        Case 4
                            xHold = "0" & Mid(xHold, 1, 1) & ":" & Mid(xHold, 2, 2)
                        Case 5
                            xHold = Mid(xHold, 1, 2) & ":" & Mid(xHold, 3, 2)
                    End Select
                    xHold = xHold & ":00"
                    xTime = "Not a time"
                    On Error Resume Next
                        xTime = TimeValue(xHold) + IIf(xLast = "p", 0.5, 0)
                    On Error GoTo 0
                    If xTime <> "Not a time" And xTime < 1 Then
                        Application.EnableEvents = False
                            xCell = xTime
                        Application.EnableEvents = True
                    End If
                End If
            End If
        End If
    Next
End If

End Sub

Open in new window

Regards,
Brian.
Convert-Time.xlsm
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38795536
Folks,

There are a few differences (of interpretation?) between ssaqibh (first time) and myself (second time) - I'v shown the times in 24-hour clock to make them clear...
(1) 1234a    --> 00:34              12:34  
(2) 1234p    --> 12:34              Invalid (so no change).
(3) 299a      --> VBA error      Invalid (so no change).

I think I prefers ssaqibh's choice for (1) and (2), but that's Robert's call.

Another difference is that mine handles multiple updates, whereas they cause a VBA error in ssabih's.

Regards,
BRian.
0
 

Author Comment

by:rws1
ID: 38795538
Thank you so much your code it works, however, one more thought if I enter more than one entry in a column and delete them all at once, or enter a wrong entry, ie, 656aa. Then the error debug window pops up any thoughts to stop that would appreciated.

Please see your attached and edited file.

Once again, thank you,
Robert
Timecon-a.xlsm
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 26

Expert Comment

by:redmondb
ID: 38795560
Robert,

Please see my version. - it doesn't mind multiple changes.

Regards,
Brian.
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 300 total points
ID: 38795563
Try changing to
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then
        For Each cel In Target.Cells
            Call Worksheet_Change(cel)
        Next cel
    Else
        If Target.Column = 4 Or Target.Column = 6 Or Target.Column = 10 Then
            If Right(Target, 1) = "p" Or Right(Target, 1) = "a" Then
                Target = CVDate(Replace(Left(Target, Len(Target) - 1) / 100, ".", ":") & Right(Target, 1) & "m")
            End If
        End If
    End If
End Sub

Open in new window

0
 

Author Comment

by:rws1
ID: 38795575
Thank you so much , both of your codes worked great.
Robert
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38795576
ssaqibh - to avoid problems with invalid dates, you may want to insert an "On Error Resume Next" before the CVDate line.
0
 

Author Closing Comment

by:rws1
ID: 38795581
Excellent is an understatement, thank you, not sure how to proceed with the points, hope it is correct and no one is offended.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38795582
Thanks, Robert, but please watch out for the different results (apparently a difference in workings between the two Excel functions, TimeValue and CVDate).

Regards,
Brian.
0
 

Author Comment

by:rws1
ID: 38795642
ssaqibh, could you please send a code with the On Error Resume Next statement in the code?

to avoid problems with invalid dates, you may want to insert an "On Error Resume Next" before the CVDate line.

Thank you
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38797753
rws1,

In case ssaqibh doesn't notice your request...
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then
        For Each cel In Target.Cells
            Call Worksheet_Change(cel)
        Next cel
    Else
        If Target.Column = 4 Or Target.Column = 6 Or Target.Column = 10 Then
            If Right(Target, 1) = "p" Or Right(Target, 1) = "a" Then
                On Error Resume Next
                    Target = CVDate(Replace(Left(Target, Len(Target) - 1) / 100, ".", ":") & Right(Target, 1) & "m")
                On Error GoTo 0
            End If
        End If
    End If
End Sub

Open in new window

Regards
Brian.
0
 

Author Comment

by:rws1
ID: 38798581
Thank you Brian
Robert
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now