• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

Convert numbers to time

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
rws1
Asked:
rws1
  • 7
  • 7
  • 3
2 Solutions
 
redmondbCommented:
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
 
rws1Author Commented:
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
 
Saqib Husain, SyedEngineerCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rws1Author Commented:
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
 
Saqib Husain, SyedEngineerCommented:
Try this file
Timecon.xlsm
0
 
redmondbCommented:
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
 
redmondbCommented:
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
 
rws1Author Commented:
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
 
redmondbCommented:
Robert,

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

Regards,
Brian.
0
 
Saqib Husain, SyedEngineerCommented:
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
 
rws1Author Commented:
Thank you so much , both of your codes worked great.
Robert
0
 
redmondbCommented:
ssaqibh - to avoid problems with invalid dates, you may want to insert an "On Error Resume Next" before the CVDate line.
0
 
rws1Author Commented:
Excellent is an understatement, thank you, not sure how to proceed with the points, hope it is correct and no one is offended.
0
 
redmondbCommented:
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
 
rws1Author Commented:
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
 
redmondbCommented:
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
 
rws1Author Commented:
Thank you Brian
Robert
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 7
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now