“文本到列”和数据透视表不是解决方案!
源数据
myval: value1
mydate: 11:11:2001
myname: bob diamond
mynum: 5648
endmarker
myval: value2
mydate: 10:10:2008
myname: jimmy knapp
mynum: 6661
endmarker
进入
myval mydate myname mynum
value 1 11:11:2001 bob diamond 5648
value 2 10:10:2008 jimmy knapp 6661
第一部分很好,其中分隔符也包含在日期中,可以通过以下方式实现:
=IFERROR(LEFT(A1,FIND(":",A1,1)-1),"")
=IFERROR(RIGHT(A1,LEN(A1)-FIND(":",A1,1)),"")
只根据第一个分隔符就可以很好地进行拆分
谷歌搜索“列表到表格”或“列表到交叉表”会返回完全相反的结果
编辑:每条记录大约有 200 个字段,这些值总是以相同的顺序出现,但有些值并不总是存在,这意味着它不能直接用 OFFSET 命令之类的东西来解决,否则它会起作用
答案1
假设你的源数据在列中A的工作表1:
首先将列标题放在工作表2
然后运行这个简短的宏:
Sub DataReOrganizer()
Dim s1 As Worksheet, s2 As Worksheet
Dim N As Long, i As Long, K As Long, v As String
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
N = s1.Cells(Rows.Count, "A").End(xlUp).Row
K = 2
For i = 1 To N
v = s1.Cells(i, "A").Text
If v = "endmarker" Then
K = K + 1
Else
ary = Split(v, ": ")
MsgBox ary(1)
If ary(0) = "myval" Then s2.Cells(K, 1) = ary(1)
If ary(0) = "mydate" Then s2.Cells(K, 2) = Chr(39) & ary(1)
If ary(0) = "myname" Then s2.Cells(K, 3) = ary(1)
If ary(0) = "mynum" Then s2.Cells(K, 4) = ary(1)
End If
Next i
End Sub
生产这个工作表2:
这铬(39)用于保留日期格式并防止 Excel 将其更改为时间。