2016-02-12 107 views
0

我有一個Excel表格,其中一列中有主鍵,另一列中是屬性列表(管道分隔符)。這裏是一個例子:Excel列表解析表

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

這繼續約38k行。

我想創建一個交叉引用表了這個數據,使其看起來更像是這樣的:

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

是否有一個簡單的方法來做到這一點?

回答

0

使用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