我设法达到了一个数据集,其中有一个用“ |”定界的项目列表符号。我现在正在尝试将列表中的每个项目都分成相应的列,但是该列的标识符在每个可变长度值的末尾都是一些文本。
示例数据(全部集中在一栏中):
Column A
40.00A|24.00QS|8.00J[a]
40.00A|12.00J|8.00J[a]
20.00A|4.00V
30.00A|12.00CS|8.00QS
所需结果:
+-------+-------+------+-------+-------+------+
| A | QS | J[a] | J | CS | V |
+-------+-------+------+-------+-------+------+
| 40.00 | 23.00 | 8.00 | | | |
| 40.00 | | 8.00 | 12.00 | | |
| 20.00 | | | | | 4.00 |
| 30.00 | 8.00 | | | 12.00 | |
+-------+-------+------+-------+-------+------+
定义列的尾随字符数固定为6(A,QS,J [a],J,CS和V),因此我一开始就知道需要多少列。
我对如何通过公式直接执行操作有一些想法,但是这将需要我通过定界符将各项拆分为单独的列,然后在某些其他列上使用某种if语句。希望避免出现辅助列问题。另外,查看下面的链接,但它不能解决问题,因为它假定值与列标题匹配(我可以纠正这一点,但是我觉得这里有一个更快的VBA解决方案): How to split single column (with unequal values) to multiple columns sorted according to values from the original single column?
我一直在阅读正则表达式,我怀疑那里有解决方案,但我不太想知道如何对结果进行排序。
一旦我完成了数据设置,通过Power Query取消数据透视并以适当的表格格式获取数据是一个小任务。
谢谢!
答案 0 :(得分:0)
由于标头是固定的,因此可以像这样简单地尝试(源和目标数据的行和列可能会更改为您的要求)
Option Explicit
Sub test()
Dim Ws As Worksheet, SrcLastrow As Long, TrgRow As Long, Rw As Long
Dim Headers As Variant, xLine As Variant
Dim i As Long, j As Long
Set Ws = ThisWorkbook.ActiveSheet
'Column A assumed to have the texts
SrcLastrow = Ws.Range("A" & Rows.Count).End(xlUp).Row
TrgRow = 2
Headers = Array("A", "QS", "J[a]", "J", "CS", "V")
For Rw = 1 To SrcLastrow
xLine = Split(Ws.Cells(Rw, 1).Value, "|")
For i = 0 To UBound(xLine)
For j = 0 To UBound(Headers)
xLine(i) = Trim(xLine(i))
If Right(xLine(i), Len(Headers(j))) = Headers(j) Then
Ws.Range("D" & TrgRow).Offset(0, j).Value = Replace(xLine(i), Headers(j), "") ' The output data table was assumed to be at Column D
End If
Next j
Next i
TrgRow = TrgRow + 1
Next
End Sub