从文本文件中获取以下文本格式:
姓名:Test1,电子邮件:Test1 @ email.com,电话:81273807,年龄:22岁,性别: 男,姓名:Test2,电子邮件:Test2 @ email.com,电话:81453807,年龄: 32,性别:女.....等
我想在excel中导入,看起来像这样:
> Name | Email | Phone | Age | Gender |
> Test1 | Test1@email.com | 81273807 | 22 | Male |
> Test2 | Test2@email.com | 81453807 | 32 | Female |
> etc
无论如何通过以某种方式格式化文本文件来做到这一点?可以避免VBA吗?
答案 0 :(得分:1)
正如我在评论中所说,您可以将,Name:
替换为\nName:
(新行/回车)。
这在Notepad ++(免费程序)中非常容易实现
https://notepad-plus-plus.org/
确保将Search Mode
设置为Extended
使用查找:,Name:
替换为:\nName:
当您使用Delimited
Comma
时,会出现这种情况
答案 1 :(得分:1)
代码:
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。