将包含天,小时和分钟的string转换为hh:mm:ss
我有400个logging要处理,它们是以下格式(string):
3h 24h20min 3h 2d 26min 1h12min 17h35min 6h12min 30s
我该如何制作一个能自动检测d
, h
, min
和s
的公式,并将hh:mm:ss
转换成正确的hh:mm:ss
,最终高于24?
我以前的答案会让你成为你的一部分。
稍作调整就是:
=VALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B5,"d"," "),"h",":"), "min",":"),"s",""))
然后将单元格格式化为[h]:mm:ss
,其中[h]
表示允许小于24的小时数(而不是包装到零)。
我并不是说这个公式适用于你所有的情况。 事实上,当你只有几分钟,一秒钟,几分钟,几分钟,但没有几小时等等时,它就会失败。但是,你要求“帮助或线索”,这确实会给你一个devise一个合适的公式的起点为了你的情况
编辑 Arrrrrhhhh,我无法抗拒。 我做了一个VBA用户定义函数来分析你的datestring。 它是相当健壮的,适用于所有的例子甚至更多 – 甚至包括随机字符的string,例如6d 243min + 7s
。 请注意,您仍然必须将单元格格式化为[h]:mm:ss
。
Function ParseDateTime(sTime As String) As Date Dim i As Long Dim identifierPos As Long Dim iTimeUnit As Long Dim nTimeUnit As Long Dim timeUnitCount As Long Dim timeUnitIdentifier() As String Dim timeUnitDateValue() As Date Dim thisDate As Date ' What are we looking for in the string? ReDim timeUnitIdentifier(1 To 4) timeUnitIdentifier(1) = "d" timeUnitIdentifier(2) = "h" timeUnitIdentifier(3) = "min" timeUnitIdentifier(4) = "s" ' What does each of these identifiers mean? ReDim timeUnitDateValue(1 To 4) timeUnitDateValue(1) = 1 ' value of 1 means 1 day in Date type. timeUnitDateValue(2) = TimeSerial(1, 0, 0) timeUnitDateValue(3) = TimeSerial(0, 1, 0) timeUnitDateValue(4) = TimeSerial(0, 0, 1) nTimeUnit = UBound(timeUnitIdentifier) ' Treat each time unit separately For iTimeUnit = 1 To nTimeUnit ' Try to locate this time unit's identifier identifierPos = InStr(sTime, timeUnitIdentifier(iTimeUnit)) If identifierPos > 0 Then ' Found it. Extract the digits that precede the identifier. For i = identifierPos - 1 To 1 Step -1 If Not (Mid(sTime, i, 1) Like "[0-9]") Then Exit For End If Next i timeUnitCount _ = CLng(Mid(sTime, i + 1, identifierPos - i - 1)) thisDate = thisDate _ + timeUnitCount * timeUnitDateValue(iTimeUnit) Else ' Identifier not found. Do nothing. End If Next iTimeUnit ParseDateTime = thisDate End Function
这个公式适用于你所有的例子
=SUM(MID(0&A1&"0000",FIND({"s","m","h","d"},0&A1&"xxsmhd")-2,2)/{86400,1440,24,1})
假设单元格A1中的数据格式化结果单元格为[h]:mm:ss
如果你有一个不在开始的数字值,它会失败,所以如果你有12 12h03min
就可以,但是如果你有12 12h3min
,公式将会失败。 我可以解决这个问题,虽然……