将一列数据拆分为另外两列

时间:2018-08-03 03:40:41

标签: excel vba

我想使用VBA将Excel数据从单列更改为多列。

我的输入数据(A1列):

Input

我希望我的输出(C1和D1列)

Output

1 个答案:

答案 0 :(得分:0)

尝试此代码

Sub Test()
Dim a, e, x

a = Sheet1.Range("A1:A" & Sheet1.Cells(Rows.Count, 1).End(xlUp).Row).Value

With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    .Item("Item") = "Count"

    For Each e In a
        If e <> "" Then
            For Each x In Split(e, ", ")
                .Item(Trim(x)) = .Item(Trim(x)) + 1
            Next x
        End If
    Next e

    a = Application.Transpose(Array(.keys, .items))
End With

Sheet1.Range("C1").Resize(UBound(a, 1), 2).Value = a
End Sub