如何根据内容将列中的数据分组到行中

how to group data from a column into rows based on content

在Excel中,我有一列文件名。每个文件名作为一个唯一的前缀,我想将具有匹配前缀的列中的所有单元格转置到一个唯一的行中。没有固定数量的条目具有匹配的前缀,因为我看到了满足该条件的解决方案。

例如,我在这样的列中有数据:

col1 col2 col3
Apple_1
Apple_2
Apple_3
Boy_1
Cow_1
Cow_2

我想将其分组并转置成这样的行:

col1 col2 col3
Apple_1 Apple_2 Apple_3
Boy_1
Cow_1 Cow_2

我想我可以在 VBA 中做到这一点,但我希望有一些方法可以在没有自定义编码的情况下做到这一点。

编辑 - 基本上,这是我需要在 VBA 宏中做的事情:

FoundCnt = 0
CurrentCell = A1
Get the string of chars up until the _ and store in CurrentPrefix 
Do {
    NextCell = CurrentCell +1
    Get the chars in NextCell up until the _  
    store in NextPrefix
    If CurrentPrefix == NextPrefix {
        move content of NextCell up 1 and to the right to B FoundCnt + 1
        FoundCnt++
        delete Row containing NextCell
    } else {
        FoundCnt = 0
    }
    CurrentCell = NextCell
}  Until NextCell == ""

请试试这个 - 它与将单列转换为具有固定列数的二维范围的公式没有太大区别,您只需将输出挂在行数等于行数的序列上唯一前缀和列数等于共享相同前缀的最大条目数。然后你必须检查当前输出列是否小于匹配当前前缀的条目数:

=LET(range,A2:INDEX(A:A,COUNTA(A:A)),
uniques,UNIQUE(LEFT(range,FIND("_",range))),
cols,MAX(COUNTIF(range,uniques&"*")),
seq,SEQUENCE(COUNTA(uniques),cols,0),
IF(MOD(seq,cols)<COUNTIF(range,uniques&"*"),INDEX(range,MATCH(INDEX(uniques,INT(seq/cols)+1)&"*",range,0)+MOD(seq,cols)),""))