将Excel数据转换为自定义格式

时间:2015-07-08 03:37:56

标签: excel vba excel-vba xslt analysis

我正在尝试将大型Excel输入表转换为自定义格式,也作为Excel表格。在视觉上这是我需要完成的事情:

enter image description here

我想出的解决问题的伪代码如下:

  1. 伪代码:
  2. //列A的级别高于列B

    Initialize dictionary
    Read the entire spreadsheet into system memory 
        While(sheet has records) { 
            Loop through spreadsheet records top-down
            Start at cell A1 
            Look to the immediate right of column A 
                if(A:B not already in dictionary){  
                Dictionary>> Append B as child of A in dictionary // Must find correct entry and append value 
                }
            Move to cell A+1
            }
        Once sheet is out of records {
                Move one column to the right
                Repeat while method
                Do this until entire column is null
         }
         while (dictionary has records) {
           Key = Column A value
           List of values = Column B value
           Save values as new Excel sheet
         }
        end
    

    我不确定是否存在可以完成我需要的库,我可以使用任何语言提供解决方案。

    感谢大家的任何意见。

1 个答案:

答案 0 :(得分:1)

将“原始数据”填入二维变量数组并循环遍历每个排名,根据具体情况构建父级或次级子级的子级。

Sub collate_family_values()
    Dim v As Long, w As Long, vVALs As Variant
    Dim sPAR As String, sTMP As String

    With ActiveSheet    '<-set this worksheet reference properly!
        .Columns("f:g").EntireColumn.Delete
        .Cells(1, 6) = "Output Data"
        .Cells(2, 6).Resize(1, 2) = .Cells(2, 1).Resize(1, 2).Value

        vVALs = Application.Transpose(.Range(.Cells(3, 1), .Cells(Rows.Count, 4).End(xlUp)).Value)

        For w = LBound(vVALs, 1) To UBound(vVALs, 1) - 1
            sTMP = ChrW(8203)
            sPAR = vVALs(w, LBound(vVALs, 2))
            For v = LBound(vVALs, 2) To UBound(vVALs, 2)
                If Not CBool(InStr(1, sTMP, ChrW(8203) & vVALs(w + 1, v) & ChrW(8203), vbTextCompare)) Then
                    sTMP = sTMP & vVALs(w + 1, v) & ChrW(8203)
                End If
                If sPAR <> vVALs(w, Application.Min(v + 1, UBound(vVALs, 2))) Or v = UBound(vVALs, 2) Then
                    .Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Resize(1, 2) = _
                      Array(sPAR, Replace(Mid(sTMP, 2, Len(sTMP) - 2), ChrW(8203), ", "))
                    sTMP = ChrW(8203)
                    If v < UBound(vVALs, 2) Then sPAR = vVALs(w, v + 1)
                End If
            Next v
        Next w
    End With
End Sub

由于要调整的行数未知,我将“输出数据”结果移到了“原始数据”的右侧。

variant array parse children