=24*SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"Hour",""),"Minute",""),"Second",""),"s",""),"-",""))," ",":")
Not all cells will always have Hours, Minutes, Seconds.Will there always be an hour, minute, and second?
So if it was 55 minutes, would it say this:
0 hours - 55 minutes - 0 seconds
Here is a sample. The data to be converted is in Column L. The Header of "Est Duration" is L5.1) Do you want VBA or a formula ?
2) Show us a sample of your data including Row & Column references, and showing all possible combinations.
Does this help any?Here is a sample. The data to be converted is in Column L. The Header of "Est Duration" is L5.
This particular data set goes to row 2851.
VBA or Formula question....VBA would be good.
Thank you for all the help
View attachment 108683
So this will work, as long as there are Hours, Minutes and Seconds. However will not work if, for example, there is only say...48 Minutes, with no hours or seconds.I think this will work...
Excel Formula:=24*SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"Hour",""),"Minute",""),"Second",""),"s",""),"-",""))," ",":")
Function ConvertTimeStringToHours(inputString As String) As Double
Dim hours As Double
Dim minutes As Double
Dim seconds As Double
Dim parts() As String
parts = Split(inputString, " ")
Dim i As Integer
For i = LBound(parts) To UBound(parts)
If UCase(parts(i)) = "HOUR" Then
hours = Val(parts(i - 1))
ElseIf UCase(parts(i)) = "MINUTES" Then
minutes = Val(parts(i - 1))
ElseIf UCase(parts(i)) = "SECONDS" Then
seconds = Val(parts(i - 1))
End If
Next i
ConvertTimeStringToHours = hours + minutes / 60 + seconds / 3600
End Function
Just noting that the lower bound for an array created by the Split function is always 0.Try this UDF.
VBA Code:parts = Split(inputString, " ") Dim i As Integer For i = LBound(parts) To UBound(parts)
I know... I posted that before you told us that your values did not always contain all three parts of the time. By the way, are you still using xl2016 or have you upgraded since putting that in your profile?So this will work, as long as there are Hours, Minutes and Seconds. However will not work if, for example, there is only say...48 Minutes, with no hours or seconds.
Thanks for the help
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.Here is a sample. The data to be converted is in Column L. The Header of "Est Duration" is L5.
This particular data set goes to row 2851.
VBA or Formula question....VBA would be good.
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