如何将数据从一列分离到多列?

How can I Separate Data from one column to Multicolumn?

提问人:ppuynoon 提问时间:8/4/2023 最后编辑:Mayukh Bhattacharyappuynoon 更新时间:8/4/2023 访问量:65

问:

我想在下面分开。

enter image description here

转换为此格式

enter image description here

我可以先分开帐户。

enter image description here

我想要运行VBA的优化方法。

谢谢。

VBA -公式 Excel-2007 导出到 Excel

评论

0赞 JohnM 8/4/2023
如果您已经可以“先分离帐户”(使用 VBA?),那么请将您的代码发布为文本(而不是图像),我还建议将您的源数据(即从第一张图像)添加为文本......可能作为表格

答:

0赞 Mayukh Bhattacharya 8/4/2023 #1

您是否真的在使用 Excel-2007,也因为您也标记了 Excel-Formula,因此这里有一种使用专用于 MS365 用户的功能的方法:

enter image description here


• 细胞中使用的配方C1

=LET(
     a,A1:A13,
     b,SCAN("",a,LAMBDA(x,y,IF(LEFT(y)=":",x,y))),
     r,ROWS(UNIQUE(FILTER(TEXTBEFORE(a,":",2),LEFT(a)=":"))),
     ub,UNIQUE(b),
     DROP(IFNA(REDUCE("",ub,LAMBDA(m,n,
          VSTACK(m,IFERROR(HSTACK(n,WRAPROWS(
          DROP(FILTER(a,n=b,""),1),r)),n)))),""),1))

0赞 FaneDuru 8/4/2023 #2

请测试下一个 VBA 解决方案。它假定要处理的范围存在于 A:A 列中,并且从“F1”开始返回。可以很容易地调整代码以返回任何位置(在工作表上、在不同的工作表上、在不同的工作簿上)。

它使用数组并主要在内存中工作,立即删除处理后的结果,在代码末尾,即使对于大范围,它也应该非常快。它与任何Excel版本兼容:

Sub SplitDataPerColums()
  Dim sh As Worksheet, lastR As Long, arr, arrFin, i As Long, j As Long, k As Long, boolOK As Boolean
  
  Set sh = ActiveSheet ' please, use here the necessary sheet
  lastR = sh.Range("A" & sh.rows.count).End(xlUp).row
  
  arr = sh.Range("A1:A" & lastR).Value2  'place the range in an array for faster processing
  ReDim arrFin(1 To UBound(arr), 1 To 3) 'maximum possible number of rows
  k = 1 'first row of the final array
  
  For i = 1 To UBound(arr)
    If Len(arr(i, 1)) = 8 And left(arr(i, 1), 3) = "ACC" Then
        boolOK = False 'to increment the row variable (k) for the case of an account without values
        arrFin(k, 1) = arr(i, 1): j = 1
        Do While InStr(arr(i + j, 1), ":") > 0
            boolOK = True
            arrFin(k, 2) = arr(i + j, 1)
            arrFin(k, 3) = arr(i + j + 1, 1)
            k = k + 1
            j = j + 2: If i + j > UBound(arr) Then Exit Do
        Loop
        If Not boolOK Then k = k + 1 'for the case of accounts without following values
        i = i + j - 1
    End If
  Next i
  Range("F1").Resize(k - 1, 3).Value2 = arrFin
  
End Sub

评论

0赞 FaneDuru 8/7/2023
@ppuynoon 你没有找到一些时间来测试上面的代码吗?如果经过测试,它没有达到您的需求吗?如果是这样,另一个答案不是也解决了你的问题吗?最好测试它们并发送一些反馈。如果一个答案比另一个答案更好,那么,我们在这里勾选代码左侧复选框以使其成为可接受的答案
0赞 ppuynoon 8/8/2023
我今天做了实验,它很实用!非常感谢您通过这个新功能扩展了我的知识。