簡體   English   中英

如何從 excel 單元格中提取日期時間?

[英]How to extract date time from an excel cell?

我有一個 excel 文件,其中的單元格包含一些注釋,例如:

txxxxx:2019 年 10 月 15 日上午 11:38:48 - 客戶 ID:xxxxx ) 第一個聯系人 - 僅發送文本至 Mob TN xxxxxxw/Ref &TN


Txxxxxx:2019 年 10 月 18 日下午 1:34:12 - 被稱為 BEST CBR xxxxxx,與先生交談,他說他們一直很忙,無法審查/討論。 主動向他發送我們的直接信息,他們將在網上查詢和/或很快給我們打電話。

短信已成功發送至 xxxxxx 的 Gull Family

下周最后的 CB 好了。

文本可以是任何可能包含多個日期時間字段的內容。 我正在嘗試從每個單元格中提取所有此類日期出現並將它們放在不同的列中

我嘗試使用=regExFind=regExExtract 例如:

=RegExFind($Cell,"\d{2}/\d{2}/\d{4}")

我也試過=Text($cell, dd/mm/yyyy)

但是,這兩種方法都不起作用。 excel 中有沒有辦法進行正則表達式提取,如果有,如何實現? 如果沒有,提取所有日期時間字段的最佳方法是什么?

更新:這是我使用的代碼:

Function RegexExtract(ByVal text As String, _
                      ByVal extract_what As String, _
                      Optional separator As String = ", ") As String

Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
Dim i As Long, j As Long
Dim result As String

RE.pattern = extract_what
RE.Global = True
Set allMatches = RE.Execute(text)

For i = 0 To allMatches.count - 1
    For j = 0 To allMatches.Item(i).submatches.count - 1
        result = result & (separator & allMatches.Item(i).submatches.Item(j))
    Next
Next

If Len(result) <> 0 Then
    result = Right$(result, Len(result) - Len(separator))
End If

RegexExtract = result

End Function

但沒有 Output。

由於您的帖子不是很明確,因此我的回答也不能再多。 試試這個:

Dim regex As Object, str As String
Set regex = CreateObject("VBScript.RegExp")

With regex
    .Pattern = "([0-9]+)/([0-9]+)/([0-9]+)"
    .Global = True
End With

str = "Whatever string you have"

Set matches = regex.Execute(str)

For Each match In matches
    Debug.Print match.Value
Next match

所以你需要在你的范圍內循環它。 str應該是您在循環中的單元格,而不是Debug.Print您應該將此值帶到Worksheet("?").Cells(i,j).Value = match.Value類的任何單元格中。

希望能幫助到你

一個解決方法...而不是使用 RegEx,想法是在單元格中找到“AM”和“PM”,然后將 19/20 字符的字符串復制粘貼到“日期提取”列中。 此方法的限制之一顯然是只有當 AM 和 PM 始終以消息的日期格式出現時才有效。

Sub ExtractDates()
Dim myRange, cell As Range
Dim StringInCell, MyDate As String
Dim DateExtrColNum, i, j As Integer


 Set myRange = Worksheets("YourSheetName").UsedRange
 DateExtrColNum = myRange.Columns(myRange.Columns.Count).Column
 Cells(1, DateExtrColNum + 1).Value = "Date Extraction"
 j = 2

 For Each cell In myRange

   If Not cell.Find("AM") Is Nothing Or Not cell.Find("PM") Is Nothing Then
   StringInCell = cell.Value
   i = 1
   Do While InStr(i, StringInCell, "AM") <> 0 Or InStr(i, StringInCell, "PM") <> 0
   If InStr(i, StringInCell, "AM") <> 0 Then
   MyDate = Mid(StringInCell, InStr(i, StringInCell, "AM") - 20, 20)
   If InStr(1, MyDate, ":") = 1 Then
   MyDate = Right(MyDate, 19)
   End If
   i = InStr(i, StringInCell, "AM") + 1
   Else: MyDate = Mid(StringInCell, InStr(i, StringInCell, "PM") - 20, 20)
   If InStr(1, MyDate, ":") = 1 Then
   MyDate = Right(MyDate, 19)
   End If
   i = InStr(i, StringInCell, "PM") + 1
   End If
   Cells(j, DateExtrColNum + 1).Value = MyDate
   j = j + 1
   Loop
   End If
Next

End Sub

暫無
暫無

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

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