我有400個記錄,處理和他們在以下格式(字符串):轉換包含天,小時和分鐘爲HH字符串:mm:ss的
3h
24h20min
3h
2d
26min
1h12min
17h35min
6h12min
30s
我怎樣的公式,其自動檢測d
,h
,min
和s
,並將hh最終高於24時轉換爲正確的hh:mm:ss
?
我有400個記錄,處理和他們在以下格式(字符串):轉換包含天,小時和分鐘爲HH字符串:mm:ss的
3h
24h20min
3h
2d
26min
1h12min
17h35min
6h12min
30s
我怎樣的公式,其自動檢測d
,h
,min
和s
,並將hh最終高於24時轉換爲正確的hh:mm:ss
?
這個previous answer of mine會讓你的一部分。
的輕微調整是:
=VALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B5,"d"," "),"h",":"),
"min",":"),"s",""))
,然後格式化單元作爲[h]:mm:ss
,其中[h]
裝置允許小時大於24號(而不是纏繞到零)。
我並不是說這個公式適用於你所有的情況。事實上,當你只有幾分鐘的時間,只有幾秒鐘,幾分鐘而沒有幾小時的時間時,它就會失敗。但是,你要求「幫助或線索」,這確實會給你一個設計適當公式的出發點爲你的情況。
編輯 Arrrrrhhhh,我無法抗拒。我做了一個VBA用戶定義的函數來分析你的日期字符串。它非常強大,適用於所有示例和更多內容 - 即使是帶有隨機字符的字符串,例如, 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
它如果你有一個數字值不是在開始時失敗,所以如果你有12h03min
這將是確定的,但如果你有12h3min
該公式將失敗。我大概可以解決這個問題,雖然......
一如既往的令人難以置信的公式:) – JMax 2013-03-11 10:48:28
例:6周31天24小時60分鐘
公式:
= ((IFERROR(VALUE(MID(J2,IF((FIND("weeks",A1)-3)=0,1,
(FIND("weeks",A1)-3)),2)),0))*(60*60*7*24))+
((IFERROR(VALUE(MID(A1,IF((FIND("days",A1)-3)=0,1,
(FIND("days",A1)-3)),2)),0))*(60*60*24))+
((IFERROR(VALUE(MID(A1,IF((FIND("hours",A1)-3)=0,1,
(FIND("hours",A1)-3)),2)),0))*(60*60))+
((IFERROR(VALUE(MID(A1,IF((FIND("minutes",A1)-3)=0,1,
(FIND("minutes",A1)-3)),2)),0))*(60))
哇,這只是完美工作,並在一瞬間!你是個天才! 非常感謝JF。 – 2012-02-18 06:13:56
已檢查。謝謝:-) – 2012-02-20 08:08:01