Excel将逗号分隔为向下与其他列分开

时间:2016-10-18 13:46:13

标签: excel transpose

我试图这样做: Sample data

要做到这一点还有很长的路要走: 突出显示B中的1个单元格。复制输出中的所有单元格。使用转置粘贴。然后拖动col A向下填充空白。

必须有一种更简单的方法:(

2 个答案:

答案 0 :(得分:1)

我想知道VBA如何与公式进行比较。

这是一个示例VBA,虽然可能有更聪明的方法,但它很短: -

Option Explicit
Public Sub SplitRows()

Dim a() As String
Dim r1, r2 As Range
Dim i, j, k As Integer
Set r1 = Range("A2:B4")
Set r2 = Range("A7:B1000")
j = 1

For i = 1 To r1.Rows.Count
    a = Split(r1.Cells(i, 2), ",")
    For k = LBound(a) To UBound(a)
      r2.Cells(j, 1) = r1.Cells(i, 1)
      r2.Cells(j, 2) = a(k)
      j = j + 1
    Next k
Next i

End Sub

答案 1 :(得分:1)

我在评论中声称,您也可以通过公式与几个辅助列进行此操作,并希望看到(a)是否属实,以及(b)这样做有多困难。答案是(a)是,只是和(b)非常(虽然有些人比我更熟练的复杂公式)。

结果如下: -

enter image description here

因此辅助列就像VBA中的i和j一样

Column C (i) is source row
Column D (j) is string number within source row

但要实现这一点,你需要一些长公式。

C7和C8刚刚硬编码为1。

在D8: -

=IF(C7="","",IF(D7<LEN(INDEX(B$2:B$4,C7))-LEN(SUBSTITUTE(INDEX(B$2:B$4,C7),",",""))+1,D7+1,IF(C7=ROWS($B$2:$B$4),"",1)))

在C8中: -

=IF(D8="","",IF(D8>D7,C7,C7+1))

在B7: -

=IF(C7="","",MID(","&INDEX(B$2:B$4,C7)&",",FIND("<",SUBSTITUTE(","&INDEX(B$2:B$4,C7)&",",",","<",D7))+1,FIND(">",SUBSTITUTE(","&INDEX(B$2:B$4,C7)&",",",",">",D7+1))-FIND("<",SUBSTITUTE(","&INDEX(B$2:B$4,C7)&",",",","<",D7))-1))

在A7中: -

=IF(C7="","",INDEX($A$2:$A$4,C7))

它是半动态的,因为源单元格的任何更改都将反映在目标单元格中​​。它与固定的源范围$ B $ 2:$ B $ 4相关联,但可以相当容易地更改为使用可变源范围。我没有假设数字都有7位数字,但如果你这样做会简化B7中的公式。