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

我有一个表和一个项目列表。 本质上它是一个问题跟踪工具的导出。 该表的其中一列包含逗号分隔的值。 我正在寻找一种方法来为多值条目的各个值创build单独的条目。

例子:( 这是一个简单的例子,真正的案例包含十几个专栏)

源数据:

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和标题重复的条目?

是否有公式,macros或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