Vba 为多值字段创建单独的行条目

Vba 为多值字段创建单独的行条目,vba,excel,Vba,Excel,我有一张有物品清单的桌子。本质上,它是从问题跟踪工具导出的。该表的一列包含逗号分隔的值。我正在寻找一种方法,为多值项的各个值创建单独的项 示例:(这是一个简化的示例,实际案例包含十几列) 源数据: ID | Title | Areas Affected | 1 | Issue title A | Area X, Area Y | 2 | Issue title B | Area Y, Area Z | 3 | Issue title C |

我有一张有物品清单的桌子。本质上,它是从问题跟踪工具导出的。该表的一列包含逗号分隔的值。我正在寻找一种方法,为多值项的各个值创建单独的项

示例:(这是一个简化的示例,实际案例包含十几列)

源数据:

ID | Title          |  Areas Affected  |  
1  | Issue title A  |  Area X, Area Y  |  
2  | Issue title B  |  Area Y, Area Z  |  
3  | Issue title C  |  Area X, Area Z  |  
ID | Title          |  Areas Affected  |   
1  | Issue title A  |  Area X          |  
1  | Issue title A  |  Area Y          |  
2  | Issue title B  |  Area Y          |  
2  | Issue title B  |  Area Z          |  
3  | Issue title C  |  Area X          |  
3  | Issue title C  |  Area Z          |  
我想要达到的目标:

ID | Title          |  Areas Affected  |  
1  | Issue title A  |  Area X, Area Y  |  
2  | Issue title B  |  Area Y, Area Z  |  
3  | Issue title C  |  Area X, Area Z  |  
ID | Title          |  Areas Affected  |   
1  | Issue title A  |  Area X          |  
1  | Issue title A  |  Area Y          |  
2  | Issue title B  |  Area Y          |  
2  | Issue title B  |  Area Z          |  
3  | Issue title C  |  Area X          |  
3  | Issue title C  |  Area Z          |  
现在ID和标题有重复条目是可以的吗


是否有公式、宏或VBA脚本来实现此目的?

您需要使用逗号作为分隔符拆分该列上的行。在VBA中,可以使用Split()函数返回数组。对于第一个元素,只需将其放回列表所在的单元格中。对于其他元素,为数组中的每个元素插入新行(这意味着在逗号分隔的列表中可以有n个元素),将整行复制到新行,并将第i个值放在其中。

在阅读/浏览示例代码之后,如果有人需要,下面是答案。这是实际的工作代码,与我在问题中发布的示例不符合1:1

Sub DataLobs()
    Application.ScreenUpdating = False 'Nice to have to increase the script speed. 

    Dim wsSrc As Worksheet
    Dim wsDst As Worksheet
    Dim curRowSrc As Integer
    Dim curRowDst As Integer
    Dim ttlRows As Integer
    Dim splitLob() As String

    ' Setting initial values to start rows in source and destination
    ' tables, as well as the total number of rows
    curRowSrc = 5
    curRowDst = 5
    ttlRows = 10000

    Set wsSrc = Worksheets("Source") 'whatever you worksheet is
    Set wsDst = Worksheets("Destination") 'or whatever your worksheet is called

    wsDst.Range("A5:F" & ttlRows).Clear

    ' Goes through column D in the source table
    ' and copies rows where the D cell is not blank
    ' into the destination table
    For curRowSrc = 5 To ttlRows
        If wsSrc.Range("D" & curRowSrc).Value <> "" Then ' There are some blank cells in the source table, so we are eliminating them.

            ' Split the cell value against the comma
            splitLob = Split(wsSrc.Range("D" & curRowSrc).Value, ", ") 'THIS IS WHERE @AlexandreP.Levasseur's HINT COMES INTO PLAY!

            For i = LBound(splitLob) To UBound(splitLob)
                wsDst.Range("A" & curRowDst).Value = splitLob(i)
                wsDst.Range("B" & curRowDst).Value = wsSrc.Range("A" & curRowSrc)
                wsDst.Range("C" & curRowDst).Value = wsSrc.Range("C" & curRowSrc)
                wsDst.Range("D" & curRowDst).Value = wsSrc.Range("AC" & curRowSrc)
                wsDst.Range("E" & curRowDst).Value = wsSrc.Range("AE" & curRowSrc)
                wsDst.Range("F" & curRowDst).Value = wsSrc.Range("AD" & curRowSrc)
                curRowDst = curRowDst + 1
            Next
        End If
    Next curRowSrc
End Sub
subdatalobs()
Application.ScreenUpdate=False“很高兴必须提高脚本速度。
将wsSrc设置为工作表
将wsDst设置为工作表
Dim curRowSrc作为整数
Dim curRowDst作为整数
将ttlRows设置为整数
Dim splitLob()作为字符串
'设置初始值以在源和目标中开始行
'表,以及总行数
curRowSrc=5
curRowDst=5
ttlRows=10000
将wsSrc=Worksheets(“Source”)设置为“无论您的工作表是什么”
设置wsDst=Worksheets(“Destination”)”或工作表的任何名称
wsDst.范围(“A5:F”和ttlRows)。清除
'穿过源表中的D列
'并复制D单元格不为空的行
'输入到目标表中
对于curRowSrc=5至ttlRows
如果wsSrc.Range(“D”&curRowSrc.Value“,”那么“源表中有一些空白单元格,因此我们将删除它们。”。
'根据逗号拆分单元格值
splitLob=Split(wsSrc.Range(“D”和curRowSrc.Value“,”),这就是@AlexandreP.Levasseur的提示发挥作用的地方!
对于i=LBound(splitLob)到UBound(splitLob)
wsDst.Range(“A”和curRowDst).Value=splitLob(i)
wsDst.Range(“B”和curRowDst).Value=wsSrc.Range(“A”和curRowSrc)
wsDst.Range(“C”和curRowDst).Value=wsSrc.Range(“C”和curRowSrc)
wsDst.Range(“D”和curRowDst).Value=wsSrc.Range(“AC”和curRowSrc)
wsDst.Range(“E”和curRowDst).Value=wsSrc.Range(“AE”和curRowSrc)
wsDst.Range(“F”和curRowDst).Value=wsSrc.Range(“AD”和curRowSrc)
curRowDst=curRowDst+1
下一个
如果结束
下一个curRowSrc
端接头

您将需要使用VBA执行该任务。我看不出任何使用简单Excel函数的方法。在“受影响区域”旁边有更多的列吗?@BrOSs Yep,这是一个简化的示例,实际案例有更多的列,大约8-10。@AlexandreP.Levasseur VBA也是可以接受的解决方案!将更新问题。谢谢你熟悉VBA吗?如果是的话,那么试着考虑一下解决这个任务需要使用的算法。基本上很简单。您需要使用逗号作为分隔符拆分该列上的行。在VBA中,可以使用Split()函数返回数组。对于第一个元素,只需将其放回列表所在的单元格中。对于其他元素,为数组中的每个元素插入新行(这意味着在逗号分隔的列表中可以有n个元素),复制新行上的整行,并将第i个值放入其中。冲洗并重复。我不熟悉VBA,但感谢您引导我正确的方向,您的回答满足了问题。我将发布另一个问题。一旦我有了一个VBA脚本草稿(如果我让它工作的话,可能就没有了!)@ulmas:请为回答你的问题打勾(在消息的左侧,你可以点击一个V形、灰色的勾号),那么我们都知道这个问题得到了回答。欢迎来到community@K_B是的,我一直在等有人来帮我写剧本,然后选择做自由职业者。似乎这不会发生:)谢谢你欢迎我!