【问题标题】:Excel List Parse TableExcel列表解析表
【发布时间】:2016-02-12 01:55:41
【问题描述】:

我有一个 Excel 表,其中一列中有一个主键,另一列中有一个属性列表(管道分隔)。这是一个例子:

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

这会持续大约 38k 行。

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

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

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

【问题讨论】:

    标签: excel parsing excel-formula cross-reference vba


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多