簡體   English   中英

從excel中的字符串中提取日期並復制到新行

[英]extract dates from a string in excel and copied over to a new row

如何從 Microsoft excel 單元格中的字符串中提取日期? 我有關於Cell A的以下信息

在單元格 A2 中,我有: 360485在單元格 B2 中,我有(注意:它在單個單元格中的換行文本):

 10/7 - comment 1 5/3/16 - comment 2 3/21/16 comment 3 1/26/16 - comment 4"

我想得到這樣的東西

Col A Col B Col C 360485 10/7/16 - comment 1 360485 5/3/16 - comment 2 360485 3/21/16 comment 3 360485 1/26/16 - comment 4"

@JNevill,

Col A 中的數據: 600537L Col B 中的數據

6/21/17 - text comment 1 
951396-LH/RH-951554
10/27 - text comment 2
normal text
2/5/16 - text comment 3"

結果

 Col A    Col B                      Col C
600537L 6/21/2017             - text comment 1
600537L 951396-LH/RH-951554 
600537L 27-Oct               - text comment 2
600537L normal               text
600537L 2/5/2016             - text comment 3

像下面這樣的東西會讓你進入球場:

Sub test()
    'get that ugly b2 value into an array split by line
    Dim b2Array As Variant
    b2Array = Split(Sheet1.Range("B2"), Chr(10))

    'grab the value in a2
    Dim a2Value As String
    a2Value = Sheet1.Range("A2").Value

    'loop through the array (each line in B2 and output. Making use of more `split` here to grab values
    Dim writeRow As Integer: writeRow = 1
    For Each element In b2Array
        Sheet2.Cells(writeRow, 1).Value = a2Value
        Sheet2.Cells(writeRow, 2).Value = Trim(Split(Trim(element), " ")(0))
        Sheet2.Cells(writeRow, 3).Value = Trim(Replace(element, Split(Trim(element), " ")(0), ""))
        writeRow = writeRow + 1
    Next
End Sub

假設這是在 Sheet1 上,並且您想要輸出到 Sheet2。

在 Excel 2010 或更高版本中,您可以使用 Power Query(又名 Get&Transform)執行此操作,然后在添加更多行時刷新查詢。

  • 通過linefeed character第 2 列拆分為
  • 修剪第 2 列以刪除前導和尾隨空格
  • space分割成列,但只有最左邊的空間

這是M代碼

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns
            (Source, {{"Column2", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv), 
            let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Column2"),

    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column2", type text}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Trim", each Text.Trim([Column2])),

    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column2"}),

    #"Split Column by Delimiter1" = Table.SplitColumn(#"Removed Columns", 
            "Trim", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Trim.1", "Trim.2"})
in
    #"Split Column by Delimiter1"

原始數據

在此處輸入圖片說明

結果

在此處輸入圖片說明

暫無
暫無

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

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