在Excel中,将单元格的值分隔到每一行。

huangapple go评论103阅读模式
英文:

Excel separate a cell value per row

问题

model serial numbers test value
ehp 2313000016 -43.5432
ehp 2313000002 -43.5432
ehp 2313000004 -43.5432
ehp 2313000005 -43.5432
ehp 2313000013 -45.5432
ehp 2313000014 -45.5432
ehp 2313000018 -45.5432
英文:

I have a spreadsheet with rows of data like this.

model serial numbers test value
ehp (2313000016,2313000002,2313000004,2313000005) -43.5432
ehp (2313000016,2313000002,2313000004,2313000005) -43.5432
ehp (2313000016,2313000002,2313000004,2313000005) -43.5432
ehp (2313000016,2313000002,2313000004,2313000005) -43.5432
ehp (2313000013,2313000014,2313000018) -45.5432
ehp (2313000013,2313000014,2313000018) -45.5432
ehp (2313000013,2313000014,2313000018) -45.5432

I need to run a formula, so it looks like this.

model serial numbers test value
ehp 2313000016 -43.5432
ehp 2313000002 -43.5432
ehp 2313000004 -43.5432
ehp 2313000005 -43.5432
ehp 2313000013 -45.5432
ehp 2313000014 -45.5432
ehp 2313000018 -45.5432

Basically, taking the group serial numbers and then breaking them out 1 per line.

So far I have only come up with doing it by hand.
worked up the formula to remove the beginning and ending parenthesizes.

=LEFT(RIGHT(E141,LEN(E141)-1),LEN(RIGHT(E141,LEN(E141)-1))-1)

but am stuck trying to figure out how to break out the numbers for each line?

答案1

得分: 3

你可以在这里使用<kbd>EXCEL FORMULAS</kbd>或<kbd>POWER QUERY</kbd>来完成这个任务。 假设根据问题中列出的标签没有Excel版本的限制


使用<kbd>TEXTBEFORE( )</kbd> 和<kbd>TEXTAFTER( )</kbd>


• 单元格<kbd>E2</kbd>中使用的公式:

=LET(
a,A2:A8,
b,B2:B8,
c,C2:C8,
d,TEXTAFTER(",&"&b,{"&","(","&",")"},SEQUENCE(,MAX(LEN(b)))),
e,TEXTBEFORE(d&",",{"&","(",")","&"}),
f,DROP(UNIQUE(TOCOL(e,3)),1),
HSTACK(a,f,c))


使用<kbd>REDUCE( )</kbd> 和<kbd>TEXTSPLIT( )</kbd>


• 单元格<kbd>E2</kbd>中使用的公式:

=LET(
a,A2:C8,
b,INDEX(a,,2),
c,DROP(REDUCE("",b,LAMBDA(x,y,VSTACK(x,TEXTSPLIT(y,",",{","})1)))),1),
HSTACK(INDEX(a,,1),TOCOL(UNIQUE(c),3),INDEX(a,,3)))


使用<kbd>POWER QUERY</kbd> 将是最简单的方法


要使用<kbd>POWER QUERY</kbd>执行任务,请按照以下步骤进行:


  • 首先将源范围转换为表格并相应地命名,例如,我将其命名为Table1

  • 接下来,从<kbd>Data</kbd>选项卡打开空白查询 --> <kbd>获取和转换数据</kbd> --> <kbd>获取数据</kbd> --> <kbd>从其他来源获取</kbd> --> <kbd>空白查询</kbd>

  • 上述步骤会打开<kbd>Power Query</kbd>窗口,现在从<kbd>Home</kbd>选项卡 --> <kbd>高级编辑器</kbd> --> 并粘贴以下<kbd>M-Code</kbd>,删除您看到的任何内容,然后点击<kbd>完成</kbd>

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"提取定界符之间的文本" = Table.TransformColumns(Source, {{"serial numbers", each Text.BetweenDelimiters(_, "(", ")"), type text}}),
#"通过定界符拆分列" = Table.ExpandListColumn(Table.TransformColumns(#"提取定界符之间的文本", {{"serial numbers", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "serial numbers"),
#"删除重复项" = Table.Distinct(#"通过定界符拆分列", {"serial numbers"})
in
#"删除重复项"


  • 最后,要将其导入到<kbd>Excel</kbd> --> 单击<kbd>关闭并加载</kbd> 或<kbd>关闭并加载到</kbd> --> 第一个单击将创建一个具有所需输出的<kbd>New Sheet</kbd>,而后者将提示一个窗口,询问您要放置结果的位置。

另一种替代方法:


• 单元格<kbd>E2</kbd>中使用的公式:

=DROP(
REDUCE("",TOROW(UNIQUE(TEXTBEFORE(TEXTAFTER(B2:B8,{"(",")"}),"&"))),
LAMBDA(x,y,VSTACK(x,TEXTSPLIT(y,,"&"))),1)


要将所有数据合并在一起:

=LET(
α,DROP(REDUCE("",TOROW(UNIQUE(TEXTBEFORE(TEXTAFTER(B2:B8,{"(",")"}),"&"))),
LAMBDA(x,y,VSTACK(x,TEXTSPLIT(y,,"&"))),1),
HSTACK(A2:A8,α,C2:C8))


另一种方法,但如果由于<kbd>CONCAT( )</kbd>函数的字符限制而超过,那么可能不起作用。

英文:

You can use either <kbd>EXCEL FORMULAS</kbd> or <kbd>POWER QUERY</kbd> here to accomplish this task. Assuming no Excel version constraints as per the tags listed in the question:


Using <kbd>TEXTBEFORE( )</kbd> & <kbd>TEXTAFTER( )</kbd>

在Excel中,将单元格的值分隔到每一行。


• Formula used in cell <kbd>E2</kbd>

=LET(
     a,A2:A8,
     b,B2:B8,
     c,C2:C8,
     d,TEXTAFTER(&quot;,&quot;&amp;b,{&quot;,&quot;,&quot;(&quot;,&quot;)&quot;},SEQUENCE(,MAX(LEN(b)))),
     e,TEXTBEFORE(d&amp;&quot;,&quot;,{&quot;,&quot;,&quot;)&quot;,&quot;(&quot;}),
     f,DROP(UNIQUE(TOCOL(e,3)),1),
     HSTACK(a,f,c))

Using <kbd>REDUCE( )</kbd> <kbd>TEXTSPLIT( )</kbd>

在Excel中,将单元格的值分隔到每一行。


• Formula used in cell <kbd>E2</kbd>

=LET(
     a,A2:C8,
     b,INDEX(a,,2),
     c,DROP(REDUCE(&quot;&quot;,b,LAMBDA(x,y,VSTACK(x,TEXTSPLIT(y,&quot;,&quot;,{&quot;(&quot;,&quot;)&quot;},1)))),1),
     HSTACK(INDEX(a,,1),TOCOL(UNIQUE(c),3),INDEX(a,,3)))

Using <kbd>POWER QUERY</kbd> will be the easiest approach.

在Excel中,将单元格的值分隔到每一行。


To perform the task using <kbd>POWER QUERY</kbd>, follow the steps:


  • First convert the source range into a table and name it accordingly, for this example I have named it as Table1

  • Next, open a blank query from <kbd>Data</kbd> Tab --> <kbd>Get & Transform Data</kbd> --> <kbd>Get Data</kbd> --> <kbd>From Other Sources</kbd> --> <kbd>Blank Query</kbd>

  • The above lets the <kbd>Power Query</kbd> window opens, now from <kbd>Home</kbd> Tab --> <kbd>Advanced Editor</kbd> --> And paste the following <kbd>M-Code</kbd> by removing whatever you see, and press <kbd>Done</kbd>

let
    Source = Excel.CurrentWorkbook(){[Name=&quot;Table1&quot;]}[Content],
    #&quot;Extracted Text Between Delimiters&quot; = Table.TransformColumns(Source, {{&quot;serial numbers&quot;, each Text.BetweenDelimiters(_, &quot;(&quot;, &quot;)&quot;), type text}}),
    #&quot;Split Column by Delimiter&quot; = Table.ExpandListColumn(Table.TransformColumns(#&quot;Extracted Text Between Delimiters&quot;, {{&quot;serial numbers&quot;, Splitter.SplitTextByDelimiter(&quot;,&quot;, QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), &quot;serial numbers&quot;),
    #&quot;Removed Duplicates&quot; = Table.Distinct(#&quot;Split Column by Delimiter&quot;, {&quot;serial numbers&quot;})
in
    #&quot;Removed Duplicates&quot;

在Excel中,将单元格的值分隔到每一行。


  • Lastly, to import it back to <kbd>Excel</kbd> --> Click on <kbd>Close & Load</kbd> or <kbd>Close & Load To</kbd> --> The first one which clicked shall create a <kbd>New Sheet</kbd> with the required output while the latter will prompt a window asking you where to place the result.

One more alternative approach :

在Excel中,将单元格的值分隔到每一行。


• Formula used in cell <kbd>E2</kbd>

=DROP(
      REDUCE(&quot;&quot;,TOROW(UNIQUE(TEXTBEFORE(TEXTAFTER(B2:B8,{&quot;(&quot;,&quot;,&quot;}),&quot;)&quot;))),
      LAMBDA(x,y,VSTACK(x,TEXTSPLIT(y,,&quot;,&quot;)))),1)

To get all the data merged together:

=LET(
     α,DROP(REDUCE(&quot;&quot;,TOROW(UNIQUE(TEXTBEFORE(TEXTAFTER(B2:B8,{&quot;(&quot;,&quot;,&quot;}),&quot;)&quot;))),
            LAMBDA(x,y,VSTACK(x,TEXTSPLIT(y,,&quot;,&quot;)))),1),
     HSTACK(A2:A8,α,C2:C8))

One other way, but if it crosses the character limitations because of <kbd>CONCAT( )</kbd> Function, then it might not work.

在Excel中,将单元格的值分隔到每一行。


• Formula used in cell <kbd>F2</kbd>

=HSTACK(A2:A8,
        DROP(TEXTSPLIT(CONCAT(TOROW(
             UNIQUE(TEXTBEFORE(TEXTAFTER(B2:B8,{&quot;(&quot;,&quot;,&quot;}),&quot;)&quot;)))&amp;&quot;,&quot;),,&quot;,&quot;),-1),
        C2:C8)

huangapple
  • 本文由 发表于 2023年8月4日 08:28:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定