簡體   English   中英

如何通過vba將文本轉換為日期並將總和轉換為時間?

[英]How to convert text to date and sum to time by vba?

你好嗎? 看,我正在打開一個制表符分隔的文件,其中包含有關分析樣品的日期和時間的信息。 我想對時間和日期求和,以便可以根據此信息對行進行排序。

時間和日期以字符串形式存儲在兩個不同的單元格中,就像:日期:29/11/2013 13:41:59:546

因此,我必須創建一個公式來刪除“ Date:”,並將“:546”轉換為毫秒,然后添加到數字的其余部分。 不幸的是,即使刪除了“ Date:”,我也無法將“ 29/11/2013”​​轉換為數字,因為Excel將日期解釋為數字。 這很有趣,因為在打開工作簿的情況下,如果我選擇2013年11月29日的單元格(在“日期:”之后被刪除)並按F2,然后輸入,excel將其轉換為數字。 當我嘗試通過vba進行操作時是否缺少某些東西?

我的結果應該是一個具有適當格式的數字

.numberformat="dd/mm/yyyy hh:mm:ss.000"

應該顯示29/11/2013 13:41:59.546。

我的職能是:

Function DateTimeCustomFormat(TimeCell As Range, DateCell As Range, formatTime As String, formatDate As String)

    Dim ms As Double 'Means milliseconds
    Dim msTOday As Long 'Factor to convert ms to day (ms divided by all milliseconds in a day)

    msTOday = 86400000

    Select Case formatTime

        Case "hh:mm:ss:ms(xxx)"

            ms = Val(Right(TimeCell, 3)) / msTOday
                TimeCell = Left(TimeCell, Len(TimeCell) - 4) 
                    TimeCell.NumberFormat = "0.00" '"h:mm:ss.000"
                        TimeCell = TimeCell + ms

    End Select

        Select Case formatDate

        Case "Date: dd/mm/yyyy"

                DateCell = Right(DateCell, Len(DateCell) - 6)
                    DateCell.NumberFormat = "dd:mm:aaaa"
                        'DateCell = DateCell.Value * 1

    End Select

        DateTimeCustomFormat = TimeCell + DateCell


End Function

好像您不太熟悉對象數據類型(例如Range)以及用戶定義函數可以做什么以及不應該做什么。

在您的代碼中,您將參數TimeCell作為Range,但是用字符串覆蓋了它( TimeCell = Left(TimeCell, Len(TimeCell) - 4) ),然后嘗試將NumberFormat設置為此。 字符串沒有NumberFormat ;-)。 此外,如果這是上述代碼行的目標,則用戶定義的函數不能將NumberFormat設置為單元格,也不能設置單元格值。 它只能返回一個值。 然后,此值將獲取單元格的值,該單元格包含用戶定義的函數作為公式。

DateCell也有同樣的問題。

您的代碼應獲取表示字符串形式的時間或日期的單元格值部分,然后將其轉換為日期。 因此,某些功能是可用的。

易於使用的是TimeValue和DateValue。 但是這些功能取決於日期和時間格式的系統設置。 因此,可能是他們沒有獲得正確的價值。 例如,對於日期為“ 06/07/2014”的日期並不清楚,它是06年7月還是07年6月。這取決於系統日期格式設置。

更通用的解決方案是使用TimeSerial和DateSerial。 我認為這是更好的解決方案,因為格式是精確定義的。

Function DateTimeCustomFormat(TimeCell As Range, DateCell As Range, formatTime As String, formatDate As String) As Date

    Dim ms As Double 'Means milliseconds
    Dim msTOday As Long 'Factor to convert ms to day (ms divided by all milliseconds in a day)
    Dim sTime As String, sDate As String 'String parts of the given parameters
    Dim dTime As Date, dDate As Date 'Calculated datetime values of the given parameters

    msTOday = 86400000

    Select Case formatTime
        Case "hh:mm:ss:ms(xxx)"
            ms = Val(Right(TimeCell.Value, 3)) / msTOday
            sTime = Left(TimeCell.Value, Len(TimeCell.Value) - 4)
            'dTime = TimeValue(sTime) + ms 'please read help for TimeValue
            dTime = TimeSerial(Left(sTime, 2), Mid(sTime, 4, 2), Mid(sTime, 7, 2)) + ms
        Case Else
            dTime = 0
    End Select

    Select Case formatDate
        Case "Date: dd/mm/yyyy"
            sDate = Right(DateCell.Value, Len(DateCell.Value) - 6)
            'dDate = DateValue(sDate) 'please read help for DateValue
            dDate = DateSerial(Right(sDate, 4), Mid(sDate, 4, 2), Left(sDate, 2))
        Case Else
            dDate = 0
    End Select

    DateTimeCustomFormat = dTime + dDate

End Function

用作UDF(用戶定義的函數): 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM