Excel根据单元格中的值重复行(以逗号分隔)

gudnpqoy  于 12个月前  发布在  其他
关注(0)|答案(5)|浏览(159)

我一直试图将Excel工作表的信息分成几部分,它看起来像这样:


的数据
我需要它看起来像这样:



我使用了这个JavaScript脚本,但它不起作用,因为它复制了它在D列(t)中找到的第一个值,并在单元格中找到了许多对象(第一行的情况下是4个):

Sub SplitPartsRows()
Dim rng As Range
Dim r As Long
Dim arrParts() As String
Dim partNum As Long
'## In my example i use columns A:E, and column D contains the Corresponding Parts ##

Set rng = Range("A1:AA16000") '## Modify as needed ##'

r = 2
Do While r <= rng.Rows.Count
    '## Split the value in column D (4) by commas, store in array ##
    arrParts = Split(rng(r, 4).Value, ",")
    '## If there's more than one item in the array, add new lines ##
    If UBound(arrParts) >= 1 Then '## corrected this logic for base 0 array
        rng(r, 4).Value = arrParts(0)

        '## Iterate over the items in the array ##
        For partNum = 1 To UBound(arrParts)
            '## Insert a new row ##'
            '## increment the row counter variable ##
            r = r + 1
            rng.Rows(r).Insert Shift:=xlDown

            '## Copy the row above ##'
            rng.Rows(r).Value = rng.Rows(r - 1).Value

            '## update the part number in the new row ##'
            rng(r, 4).Value = Trim(arrParts(partNum))

            '## resize our range variable as needed ##
            Set rng = rng.Resize(rng.Rows.Count + 1, rng.Columns.Count)

        Next

    End If
'## increment the row counter variable ##
r = r + 1
Loop

End Sub

字符串
结果会是这样的:



我不一定需要使用JavaScript我接受任何可能的答案,即使在谷歌应用程序脚本

zaq34kh6

zaq34kh61#

我接受任何可能的答案,即使在谷歌应用程序脚本
你所问的问题可以用一个普通的电子表格公式来完成。IkeMayukh Bhattacharya的答案展示了如何在Microsoft Excel 365中完成。要在Google Sheets中完成同样的操作,请使用以下模式:

=let( 
  fixed, A1:C, 
  dist, D1:D, 
  null, tocol(æ, 2), 
  reduce(null, dist, lambda(result, d, if(len(d), 
    vstack( 
      result, 
      reduce(null, split(d, ","), lambda(a, v, 
        vstack(a, hstack(index(fixed, row(d)), v)) 
      )) 
    ), 
    result  
  ))) 
)

字符串
参见How to unpivot data that has multiple column groups in Google Sheets?

8tntrjer

8tntrjer2#

使用data. from table/range.将数据带入powerquery。
右键单击最右侧的列,然后选择拆分列...按拆分...
选择逗号作为逗号,并在高级选项中选择行
x1c 0d1x的数据
文件。。关闭并加载。返回到excel

vulvrdjw

vulvrdjw3#

如果你有Excel 365,你可以使用这个公式:

=LET(d,A1:D3,
DROP(REDUCE("",SEQUENCE(ROWS(d)),LAMBDA(result1,i,VSTACK(result1,
           DROP(REDUCE("",TOCOL(TEXTSPLIT(INDEX(d,i,4),",")),
                  LAMBDA(result2,x,VSTACK(result2,HSTACK(DROP(CHOOSEROWS(d,i),,-1),x)))),1)))),1))

字符串
TOCOL(TEXTSPLIT将列D值拆分为行-然后将行与相应的标题数据堆叠在一起。
x1c 0d1x的数据

wb1gzix0

wb1gzix04#

假设没有**Excel Constraints**作为每个标签张贴,那么可以使用以下公式.


的数据
·单元格A5中使用的公式

=LET(
     α, A1:D3,
     Ψ, LAMBDA(φ,δ,VSTACK(φ,IF({1,1,1,0},INDEX(α,δ,0),TEXTSPLIT(INDEX(α,δ,4),,",")))),
     DROP(REDUCE(0,SEQUENCE(ROWS(α)),Ψ),1))

字符串
基本上上面的公式是:

=LET(α, A1:D3,
 DROP(REDUCE(0,SEQUENCE(ROWS(α)),LAMBDA(φ,δ,VSTACK(φ,IF({1,1,1,0},INDEX(α,δ,0),TEXTSPLIT(INDEX(α,δ,4),,","))))),1))


和更长的方法,但不使用任何LAMBDA()辅助函数:



·单元格A5中使用的公式

=LET(
     α, A1:D3,
     φ, TAKE(α,,-1),
     δ, MAX(LEN(φ)-LEN(SUBSTITUTE(φ,",",))+1),
     Ψ, TEXTSPLIT(TEXTAFTER(","&φ,",",SEQUENCE(,δ)),","),
     ε, HSTACK(INDEX(TAKE(α,,3),INT(SEQUENCE(ROWS(α)*δ,,,1/δ)),SEQUENCE(,3)),TOCOL(Ψ)),
     FILTER(ε,1-ISERROR(TAKE(ε,,-1))))

thtygnil

thtygnil5#

请测试下一个数组解决方案。它使用数组,并且主要在内存中工作,即使要处理的范围很大,也应该非常快。它处理各个工作表中的行(基于A:A列中的最后一个单元格)以及您在加载arr时选择的列数。处理后的数组内容被放入目标工作表中。以下代码在下一个工作表中返回(在活动工作表之后),因此它应该是空的。但是您可以选择任何其他工作表来执行它:

Dim ws As Worksheet, wsDest As Worksheet, lastR As Long, arr, arrSpl, arrFin, count As Long
 Dim i As Long, j As Long, m As Long, k As Long
 Const SplCol As Long = 4 'the column number containing the comma separated strings
 
 Set ws = ActiveSheet 'use here the sheet you need
 lastR = ws.Range("A" & ws.rows.count).End(xlUp).Row
 Set wsDest = ws.Next 'use here the destination sheet you need
 
 arr = ws.Range("A1:K" & lastR).value 'place the range in an array for faster processing
 
 'Determine the number of rows for the Final Array (arrFin)
 For i = 1 To UBound(arr)
    count = count + UBound(Split(arr(i, SplCol), ",")) + 1
 Next i
 
 'Redim the final array according to determined count:
 ReDim arrFin(1 To count, 1 To UBound(arr, 2))
 
 For i = 1 To UBound(arr) 'processing the array elements as required:
    arrSpl = Split(arr(i, SplCol), ",")  'split the string from SplCol
    For j = 0 To UBound(arrSpl)          'iterate between each split array elements
        k = k + 1                        'increment the row of arrFin
        For m = 1 To UBound(arr, 2)      'iterate between the array columns:
            If m = SplCol Then           'for SplCol, use the array element
                arrFin(k, m) = arrSpl(j)
            Else
                arrFin(k, m) = arr(i, m) 'for the rest of columns, use the existing values
            End If
        Next m
    Next j
 Next i
 
 'drop the processed final array, at once:
 wsDest.Range("A1").Resize(UBound(arrFin), UBound(arrFin, 2)).value = arrFin
 wsDest.Activate
 
 MsgBox "Ready..."
End Sub

字符串
请在测试后发送一些反馈。如果有些东西不够清楚,即使我尝试注解所有代码行,也不要犹豫要求澄清。

相关问题