Excel 列表解析 Table

Excel List Parse Table

我有一个 excel sheet,其中一列是主键,另一列是属性列表(竖线分隔)。这是一个例子:

pk1     a | b | c
pk2
pk3     b | d
pk4     e

这持续了大约 38k 行。

我想根据这些数据创建一个交叉引用 table,使其看起来更像这样:

pk1     a
pk1     b
pk1     c
pk3     b
pk3     d
pk4     e

有没有简单的方法可以做到这一点?

使用 VBA 此子程序以您提供的格式解析数据。可能需要根据列结构进行一些更新。

Sub Parse()
    Dim thissheet As Worksheet
    Set thissheet = ActiveSheet

    Sheets.Add
    ActiveSheet.Range("A1").Value = "Header 1"
    ActiveSheet.Range("B1").Value = "Header 2"

    Dim pk As Long
    Dim carray() As String

    For x = 0 To thissheet.Range("A65535").End(xlUp).Row

    If InStr(1, thissheet.Range("B2").Offset(x, 0).Value, "|") > 0 Then
    'if cellvalue contains bar
    carray() = Split(thissheet.Range("B2").Offset(x, 0).Value, "|")
    For i = LBound(carray) To UBound(carray)
    ActiveSheet.Range("A2").Offset(pk, 0).Value = thissheet.Range("A2").Offset(x, 0).Value
    ActiveSheet.Range("B2").Offset(pk, 0).Value = Trim(carray(i))
    pk = pk + 1
    Next i
    Else
    'No Bar do this
    ActiveSheet.Range("A2").Offset(pk, 0).Value = thissheet.Range("A2").Offset(x, 0).Value
    ActiveSheet.Range("B2").Offset(pk, 0).Value = thissheet.Range("B2").Offset(x, 0).Value
    pk = pk + 1
    End If
    Next

    End Sub