如何在一个单元格中拆分分隔数据,并针对分离出的每个分隔数据项重复相邻单元格

问题描述 投票:0回答:1

我有一个excel文件,其中包含一个单元格中的应用程序列表,以及与相邻单元格中的应用程序相对应的多个分隔用户名称。我需要在一列中将用户从一个单元格分隔到多个单元格,同时为每个用户重复应用程序的名称

    Current data looks something like this;

    Column1        | Column 2
    Application 1  | User1,User2,User3
    Application 2  | User1,User2,User3

    I want to get an output to be something like this;
    Column 1       | Column 2   
    Application 1  |  User 1
    Application 1  |  User 2
    Application 1  |  User 3
    Application 2  |  User 1
    Application 2  |  User 2
    Application 2  |  User 3

我一直在玩索引匹配,VBA等并且惨遭失败 - 我认为我迄今为止完成的任何代码都不相关

excel excel-formula rows repeat delimited-text
1个回答
0
投票

这是一个超级简单的方法:

Sub Reorg()
    Dim i As Long, N As Long, ap As String, arr, a
    Dim k As Long

    N = Cells(Rows.Count, "A").End(xlUp).Row
    k = 1

    For i = 1 To N
    ap = Cells(i, 1).Value
    arr = Split(Cells(i, 2).Value, ",")
        For Each a In arr
            Cells(k, 3).Value = ap
            Cells(k, 4).Value = a
            k = k + 1
        Next a
    Next i
End Sub

对于A列和B列中的原始数据:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.