[英]Excel Split cell with multiple lines into rows for entire table
在Excel(2016)中,我有一些看起來像這樣的數據:
基於特定列,在此示例中為“ IPAddress”列,如果單元格中有多行,則將字符串分成新行,然后將剩余數據復制到該行。
這是腳本或任何其他內容完成后要尋找的東西。
我正在使用以下代碼:將具有多行的單元格拆分為行
Sub tes_5()
Dim cell_value As Variant
Dim counter As Integer
'Row counter
counter = 1
'Looping trough A column define max value
For i = 1 To 10
'Take cell at the time
cell_value = ThisWorkbook.ActiveSheet.Cells(i, 1).Value
'Split cell contents
Dim WrdArray() As String
WrdArray() = Split(cell_value, vbLf)
'Place values to the B column
For Each Item In WrdArray
ThisWorkbook.ActiveSheet.Cells(counter, 2).Value = Item
counter = counter + 1
Next Item
Next i
End Sub
這樣可以分隔IPAddress列,但不會在新行中添加其他單元格的數據。
文本到列不起作用和Power Query( https://www.quora.com/Is-there-a-way-in-excel-to-bulk-split-cells-with-multiple-lines-inside-into -新的行,且沒有覆蓋下面的現有數據 )也不起作用。
還有其他建議嗎?
更新:剛剛了解到,默認情況下,Excel在定界符字段的開頭放置了一個逗號,這導致我在選擇換行時定界符不起作用。
如果刪除前導逗號,則“應該”(就像我一樣),獲得所需的結果。
假設您的數據從A2開始,請嘗試以下操作:
Sub x()
Dim r As Long, v As Variant
For r = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
v = Split(Cells(r, 4), vbLf)
If UBound(v) > 0 Then
Cells(r + 1, 1).Resize(UBound(v), 4).Insert shift:=xlDown
Cells(r + 1, 1).Resize(UBound(v), 3).Value = Cells(r, 1).Resize(, 3).Value
Cells(r, 4).Resize(UBound(v) + 1).Value = Application.Transpose(v)
End If
Next r
End Sub
使用Power Query非常簡單
您需要做的就是將IPAddress
列按換行符拆分為Rows
用戶界面中的“拆分列”對話框
M代碼
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Server Name", type text}, {"Serial Number", type text}, {"OS", type text}, {"IPAddress", type text}}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {{"IPAddress", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "IPAddress")
in
#"Split Column by Delimiter"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.