- 打開一個新的空白工作簿將其設置爲
Delimited
。
- 單擊Sheet1名稱選項卡並選擇查看代碼。
- 當VBE打開時,將以下內容粘貼到名爲Sheet1(Code)的代碼表中。
代碼:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Parent.UsedRange.Cells.Count = 1 And _
Target.Cells.Count = 1 And _
Target.Address(0, 0) = "A1" Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim a As Long, b As Long, arr1 As Variant, arr2 As Variant, hdr As Variant, tmp As Variant
arr1 = Split(Replace(Target.Value2, ",Name:", vbLf & "Name:"), vbLf)
For a = LBound(arr1) To UBound(arr1)
arr2 = Split(arr1(a), Chr(44))
For b = LBound(arr2) To UBound(arr2)
tmp = Split(arr2(b), Chr(58))
hdr = Application.Match(tmp(0), Rows(1), 0)
If IsError(hdr) Then
Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1) = tmp(0)
hdr = Application.Match(tmp(0), Rows(1), 0)
End If
Cells(2 + a, hdr) = tmp(1)
Next b
Next a
Columns(1).EntireColumn.Delete
End If
safe_exit:
Application.EnableEvents = True
End Sub
- 點擊按Ctrl + Q返回到你的新的空白工作表。 - 在記事本或其他文本編輯器中打開TXT文件並複製整個事情。 - 返回新的空白Sheet1並將文本粘貼到A1中。使用
Excel中導入功能,但我願意接受任何解決方案 – Dreamwatcher
您可以嘗試查找/替換',名稱:'和',[新線]產品名稱:' – user1274820
我真的很缺乏經驗與VBA – Dreamwatcher