Changing Hours, minutes, seconds to hours

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Hello
I am having trouble with vba to change:
2 Hour - 55 Minutes - 22 Seconds to an hour decimal, which equals 2.92 hours.

Any help is greatly appreciated.
Thank you
 
Cubist has already provided a Function version but since you have provided information for running a macro try this on a copy of your workbook.

At this stage it is outputting to the next empty column in the spreadsheet.
If you have validated the output and want it to overwrite column L that is a simple modification.

VBA Code:
Sub ConvertStringToTime()
   
    Dim ws As Worksheet
    Dim rngTimeTxt As Range, arr As Variant
    Dim rngOut As Range
    Dim splitTime As Variant, partTime As String
    Dim rowHdg As Long, i As Long, j As Long
    Dim hrs As Long
    Dim mins As Long
    Dim secs As Long
   
    Set ws = ActiveSheet
    rowHdg = 5
    With ws
        Set rngTimeTxt = .Range(.Cells(rowHdg + 1, "L"), .Cells(Rows.Count, "L").End(xlUp))
        arr = rngTimeTxt.Value
    End With
   
    Set rngOut = ws.Cells(rowHdg, Columns.Count).End(xlToLeft).Offset(1, 1)
   
    For i = 1 To UBound(arr)
        hrs = 0
        mins = 0
        secs = 0
        splitTime = Split(Trim(arr(i, 1)), " ")
        For j = 0 To UBound(splitTime)
            partTime = splitTime(j)
            Select Case True
                Case UCase(partTime) Like "HOUR*"
                    hrs = Val(splitTime(j - 1))
                Case UCase(partTime) Like "MINUTE*"
                    mins = Val(splitTime(j - 1))
                Case UCase(partTime) Like "SECOND*"
                    secs = Val(splitTime(j - 1))
                Case Else
                    ' do nothing
            End Select
        Next j
        arr(i, 1) = 24 * TimeSerial(hrs, mins, secs)
    Next i
   
    'rngOut.Offset(-1).Value = "Est Duration (hrs)"         <--- Leave heading for now, easier to rerun
    rngOut.Resize(UBound(arr)) = arr
End Sub
I will try this and report back. This is a significant time spent so thank you very much for your help.
Thanks!
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I will try this and report back. This is a significant time spent so thank you very much for your help.
Thanks!
It may be working, let me try some more things.
 
Last edited:
Upvote 0
I will try this and report back. This is a significant time spent so thank you very much for your help.
Thanks!
Thank you Alex (and Cubist) for the help, the vba code is working well!
 
Upvote 0
Cubist has already provided a Function version but since you have provided information for running a macro try this on a copy of your workbook.

At this stage it is outputting to the next empty column in the spreadsheet.
If you have validated the output and want it to overwrite column L that is a simple modification.

VBA Code:
Sub ConvertStringToTime()
   
    Dim ws As Worksheet
    Dim rngTimeTxt As Range, arr As Variant
    Dim rngOut As Range
    Dim splitTime As Variant, partTime As String
    Dim rowHdg As Long, i As Long, j As Long
    Dim hrs As Long
    Dim mins As Long
    Dim secs As Long
   
    Set ws = ActiveSheet
    rowHdg = 5
    With ws
        Set rngTimeTxt = .Range(.Cells(rowHdg + 1, "L"), .Cells(Rows.Count, "L").End(xlUp))
        arr = rngTimeTxt.Value
    End With
   
    Set rngOut = ws.Cells(rowHdg, Columns.Count).End(xlToLeft).Offset(1, 1)
   
    For i = 1 To UBound(arr)
        hrs = 0
        mins = 0
        secs = 0
        splitTime = Split(Trim(arr(i, 1)), " ")
        For j = 0 To UBound(splitTime)
            partTime = splitTime(j)
            Select Case True
                Case UCase(partTime) Like "HOUR*"
                    hrs = Val(splitTime(j - 1))
                Case UCase(partTime) Like "MINUTE*"
                    mins = Val(splitTime(j - 1))
                Case UCase(partTime) Like "SECOND*"
                    secs = Val(splitTime(j - 1))
                Case Else
                    ' do nothing
            End Select
        Next j
        arr(i, 1) = 24 * TimeSerial(hrs, mins, secs)
    Next i
   
    'rngOut.Offset(-1).Value = "Est Duration (hrs)"         <--- Leave heading for now, easier to rerun
    rngOut.Resize(UBound(arr)) = arr
End Sub
Just out of curiosity, you speak of a simple modification to overright the origianl data in Column L. I am doing it by cutting and inserting into Column L. is there another way to incorporate inside this?
Thanks
 
Upvote 0
I am not at a computer at the moment but if you are happy to overwrite what you had in column L, just replace this:
rngOut.Resize(UBound(arr)) = arr
With this:
rngTimeTxt.Value = arr
 
Upvote 0
@Guzzlr
If you wish to mark a post as the solution you may do so but please mark one that does have a solution, not like post 21. :)
I have removed the solution mark from that post.
 
Upvote 0
Thank you Alex (and Cubist) for the help, the vba code is working well!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top