excelparsingexcel-formulacross-referencevba

Excel List Parse Table


I have an excel sheet with a primary key in one column and a list of attributes (pipe delimited) in another. Here is an example:

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

This goes on for about 38k rows.

I want to create a cross reference table out of this data so that it looks more like this:

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

Is there an easy way to accomplish this?


Solution

  • With VBA this sub parsed the data in the format you provided. May require a little updating depending on columns structure.

    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