+ Reply to Thread
Results 1 to 3 of 3

Function or macro to convert string with weeks, days, hours, minutes to Hours

Hybrid View

  1. #1
    Registered User
    Join Date
    05-13-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    20

    Function or macro to convert string with weeks, days, hours, minutes to Hours

    Hi,

    I would like some help coming up with a function or macro that will enable me to convert the below estimates stored as strings in excel into hours. I also want to avoid using text to columns.

    Original Estimate
    1 week, 6 hours
    1 week, 1 day, 6 hours
    2 hours, 35 minutes
    2 weeks
    1 hour, 1 minute

    The results should be:
    Hours
    174
    198
    2.58
    336
    1.02

    I'm really stumped and would appreciate any help.

    Thanks!
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Function or macro to convert string with weeks, days, hours, minutes to Hours

    Place the following code in a standard module, and use like a user defined function.

    Function Convert2Hours(rg As Range) As Double
        Dim sArray() As String
        Dim i As Long
        Dim nValue As Long
        Dim dbResult As Double
        
        sArray = Split(rg.Text, ",")
        For i = LBound(sArray) To UBound(sArray)
            nValue = CLng(Val(sArray(i)))
            If InStr(1, sArray(i), "minute", vbTextCompare) > 0 Then
                dbResult = dbResult + nValue / 60
            ElseIf InStr(1, sArray(i), "hour", vbTextCompare) > 0 Then
                dbResult = dbResult + nValue
            ElseIf InStr(1, sArray(i), "day", vbTextCompare) > 0 Then
                dbResult = dbResult + nValue * 24
            ElseIf InStr(1, sArray(i), "week", vbTextCompare) > 0 Then
                dbResult = dbResult + nValue * (24 * 7)
            End If
        Next i
        Convert2Hours = dbResult
    End Function

  3. #3
    Registered User
    Join Date
    05-13-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Function or macro to convert string with weeks, days, hours, minutes to Hours

    This is exactly what I was looking for.

    Thanks for the quick reply!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1