Avatar of Surendar S
Surendar S
 asked on

how do I convert to hours?

I want to convert the below column value into hours, can you please help?

2 weeks, 1 day, 6 hours, 30minutes

(1 week = 5 days) the above column should give me result in another column in hours. Please help me with formula.
Microsoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Surendar S

8/22/2022 - Mon
Bill Prew

So are you saying the cells you want to convert have text in them, and the format of that text is always:

2 weeks, 1 day, 6 hours, 30 minutes

with that spacing, and commas, etc?  And is that always true?  And will all 4 pieces always be present, even if say minutes was 0?


»bp
Bill Prew

This would be a lot easier / simpler in a VBA macro function that you could then call from a cell formula, but since you specifically asked for a formula approach, here is a sheet with the formula in it and a couple of test cases.

The text field must be of the following format or an error will be generated:

aaa weeks, bbb days, ccc hours, ddd minutes

EE29066936.xlsx


»bp
Surendar S

ASKER
Hi Bill,

First of all thank you for your help.
These are the some possibilities for the column.

Column A
30 minutes
1 day, 1 hour
2 days, 30 minutes
1 week, 2 days, 30 minutes
3 weeks, 1 day, 1 minute
4 weeks, 2 days
1 week

Need to show the hour in Column B as you know.
And 1 day considered as 8 hours, did you already applied the logic in the formula?
can you give me the formula for the attached file? Thanks in advance.
For the empty cells, we need to show as 0 minutes/leave them as empty.
Hours-Calculation.xlsx
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Bill Prew

Okay, here is an update that should handle all of those, make sure the numbers are what you wanted.  The formula is in the attached spreadsheet, and looks like this:

=IFERROR(LEFT(TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (0)*LEN(A2)+1, LEN(A2))), FIND(" ", TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (0)*LEN(A2)+1, LEN(A2)))))*INDEX({2400,2400,480,480,60,60,1,1},MATCH(TRIM(MID(TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (0)*LEN(A2)+1, LEN(A2))), FIND(" ", TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (0)*LEN(A2)+1, LEN(A2)))),999)),{"week","weeks","day","days","hour","hours","minute","minutes"},0)),0)+IFERROR(LEFT(TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (1)*LEN(A2)+1, LEN(A2))), FIND(" ", TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (1)*LEN(A2)+1, LEN(A2)))))*INDEX({2400,2400,480,480,60,60,1,1},MATCH(TRIM(MID(TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (1)*LEN(A2)+1, LEN(A2))), FIND(" ", TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (1)*LEN(A2)+1, LEN(A2)))),999)),{"week","weeks","day","days","hour","hours","minute","minutes"},0)),0)+IFERROR(LEFT(TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (2)*LEN(A2)+1, LEN(A2))), FIND(" ", TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (2)*LEN(A2)+1, LEN(A2)))))*INDEX({2400,2400,480,480,60,60,1,1},MATCH(TRIM(MID(TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (2)*LEN(A2)+1, LEN(A2))), FIND(" ", TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (2)*LEN(A2)+1, LEN(A2)))),999)),{"week","weeks","day","days","hour","hours","minute","minutes"},0)),0)+IFERROR(LEFT(TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (3)*LEN(A2)+1, LEN(A2))), FIND(" ", TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (3)*LEN(A2)+1, LEN(A2)))))*INDEX({2400,2400,480,480,60,60,1,1},MATCH(TRIM(MID(TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (3)*LEN(A2)+1, LEN(A2))), FIND(" ", TRIM(MID(SUBSTITUTE(A2,",",REPT(" ",LEN(A2))), (3)*LEN(A2)+1, LEN(A2)))),999)),{"week","weeks","day","days","hour","hours","minute","minutes"},0)),0)

Open in new window


Hours-Calculation.xlsx


»bp
Bill Prew

And just comparison here is an approach using a User Defined Function in VBA macro to do the calculation.  Spreadsheet attached with a new column calculated using this approach, and containing the macro shown below.

Public Function CalcMinutes(strText As String) As Variant
    Dim arrChunk() As String
    Dim strChunk As Variant
    Dim arrWord() As String
    
    CalcMinutes = 0
    If strText = "" Then Exit Function
    
    arrChunk = Split(strText, ",")
    For Each strChunk In arrChunk
        arrWord = Split(Trim(strChunk), " ")
        If UBound(arrWord) <> 1 Then
            CalcMinutes = CVErr(xlErrNA)
            Exit Function
        End If
        Select Case LCase(arrWord(1))
            Case "minute", "minutes"
                If IsNumeric(arrWord(0)) Then
                    CalcMinutes = CalcMinutes + CInt(arrWord(0))
                Else
                    CalcMinutes = CVErr(xlErrNA)
                    Exit Function
                End If
            Case "hour", "hours"
                If IsNumeric(arrWord(0)) Then
                    CalcMinutes = CalcMinutes + (CInt(arrWord(0)) * 60)
                Else
                    CalcMinutes = CVErr(xlErrNA)
                    Exit Function
                End If
            Case "day", "days"
                If IsNumeric(arrWord(0)) Then
                    CalcMinutes = CalcMinutes + (CInt(arrWord(0)) * 480)
                Else
                    CalcMinutes = CVErr(xlErrNA)
                    Exit Function
                End If
            Case "week", "weeks"
                If IsNumeric(arrWord(0)) Then
                    CalcMinutes = CalcMinutes + (CInt(arrWord(0)) * 2400)
                Else
                    CalcMinutes = CVErr(xlErrNA)
                    Exit Function
                End If
            Case Else
                CalcMinutes = CVErr(xlErrNA)
                Exit Function
        End Select
    Next
End Function

Open in new window

Hours-Calculation.xlsm


»bp
Surendar S

ASKER
Hi Bill,

Thank you very much!! I am wondering by seeing that formula, It is working as expected :)
Can I ask you one more help?
Please find the attached file, we need to calculate the total time spent based on Main ticket# in the sheet of Main by comparing the same to the next sheet of Sub.

1 week = 5 days
1 day = 8 hours as you know.


Main sheet > C2 = addition of Sub sheet > C column based on Main sheet > Main ticket column.

Please give me the result C2 same like B2 by calculating all the sub tickets in the next sheet based on Main Ticket.
Remaining time we need calculate B2-C2 = D2

Thanks,
Surendar
Hours-Calculation.xlsx
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

Okay, added that in, see how this looks.

Hours-Calculation.xlsx


»bp
Surendar S

ASKER
Hi Bill,

Thank you, sorry I should have informed you earlier.
I need C,D and E column in the format of B column > weeks,days,hours,minutes

1 week - 5 days
1 day - 8 hours

Can you please help.

Thanks,
Surendar
Surendar S

ASKER
Sorry for the re-work Bill, the last request should be my final one.
I am very thankful for you.

Thanks,
Surendar
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Bill Prew

Working it...


»bp
Bill Prew

These formulas are getting very large and complicated, are you sure you want to solve this with formulas rather than VBA user defined functions?


»bp
Bill Prew

Here is what I have so far.  The calculated columns in hours (highlighted in yellow) are needed to drive the "text" versions of those, but you can of course hide those columns so they aren't visible.

(I did adjust some of the data on the second sheet just to get some more interesting values on the first sheet)

Hours-Calculation.xlsx


»bp
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Surendar S

ASKER
Hi Bill,

Thank you :)

Seems we need to correct minus values, when it crosses the original estimation.
Here is the output I have, please find the attached file.
I have added some comments for you, if possible can you please help on that.

Thanks,
Surendar
Hours-Calculation.xlsx
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Surendar S

ASKER
Hi Bill,

I don't see formula in the excel, can you please check.
The result in the excel looks fine.

Thanks,
Surendar
Bill Prew

Formula is there, but I left it as multi-line format since that makes it much easier to follow.  I typically work with large formulas like this in a text editor and then paste in to Excel.

To see the full formula you can expand the formula bar by using the small v icon (see picture below).

Or, when on the cell you can press F2 and see the full formula on top of the spreadsheet.

sshot-351.png

»bp
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Surendar S

ASKER
Thanks a lot :)
You are great!!!! I don't know what to say. I will never forget your help.