简体   繁体   English

Excel - 如何将多列中的分号分隔值拆分为行

[英]Excel - How to split semi-colon separated values in multiple columns into rows

I have a table in Excel with following format我在 Excel 中有一张表格,格式如下

Col1 Col1 Col2 Col2 Col3 Col3
A;B;C;D A;B;C;D 1;2;3;4 1;2;3;4 a;b;c;d a;b;c;d
E;F E;F 0;5 0;5 x;y x;y

How do I split each corresponding values in columns to rows?如何将列中的每个对应值拆分为行?

Col1 Col1 Col2 Col2 Col3 Col3
A一个 1 1 a一个
B 2 2 b b
C C 3 3 c c
D D 4 4 d d
E 0 0 x X
F F 5 5 y是的

I tried power query (from Table/Range) -> Split column by delimiter (Advanced -> into rows).我尝试了电源查询(来自表/范围)-> 按分隔符拆分列(高级-> 成行)。 But I quickly get a lot of duplicated values and removing duplicates can be challenging.但是我很快就会得到很多重复的值,并且删除重复项可能具有挑战性。

Any suggestions on how to achieve this without a macro?关于如何在没有宏的情况下实现这一目标的任何建议?

In powerquery在电源查询中

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes (Source,List.Transform(Table.ColumnNames(Source), each {_, type text})),
TableTransform = Table.Combine(List.Transform(List.Transform(Table.ToRecords(#"Changed Type"), (x) => List.Transform(Record.ToList(x), each Text.Split(_,";"))), each Table.FromColumns(_, Table.ColumnNames(#"Changed Type"))))
in  TableTransform

在此处输入图像描述

If you have the data in the following range: B1:D3 (including the header).如果您有以下范围内的数据: B1:D3 (包括标题)。 You can achieve it for each column as follows in F2 ;您可以在F2中为每一列实现它,如下所示;

=TEXTSPLIT(TEXTJOIN(";",,B2:B3),,";")

For the first column and then to extend the formula to the following columns.对于第一列,然后将公式扩展到以下列。

Or having all at once using HSTACK function:或者使用HSTACK function 一次性完成:

=HSTACK(
  TEXTSPLIT(TEXTJOIN(";",,B2:B3),,";"),
  TEXTSPLIT(TEXTJOIN(";",,C2:C3),,";"),
  TEXTSPLIT(TEXTJOIN(";",,D2:D3),,";")
)

示例 excel 文件

A more concise way to achieve it for a large number of columns is to use the following formula:为大量列实现它的更简洁的方法是使用以下公式:

=TRANSPOSE(
  TEXTSPLIT(REDUCE("", 
  BYCOL(B2:D3, LAMBDA(x, TEXTJOIN(";",,x))), 
  LAMBDA(a,b,IF(a="", b, a&","&b))),";",",",,,""))

使用减少的样本

Notes:笔记:

  1. Added more values to columns 1 and 2, to validate it works when we have different number of rows in the table.向第 1 列和第 2 列添加了更多值,以验证它在表中有不同行数时是否有效。
  2. We use pad_with input argument from TEXTSPLIT to consider columns with different number of elements and to pad is as blank.我们使用来自TEXTSPLITpad_with输入参数来考虑具有不同数量元素的列,并将填充为空白。
  3. REDUCE is used to convert the entire input to a single string, adding a column delimiter ( , ) REDUCE用于将整个输入转换为单个字符串,添加列分隔符 ( , )
  4. Finally using TEXTSPLIT to convert it back to an array format, we need to transpose the result because this function populates the information by row.最后使用TEXTSPLIT将其转换回数组格式,我们需要对结果进行转置,因为这个 function 按行填充信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM