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

我试图将一个大的Excelinput表格转换成一个自定义的格式,也作为一个Excel表格。 在视觉上,这是我需要完成的:

在这里输入图像说明

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

  1. 伪代码:

//列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 

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

感谢来自你们的任何意见。

将“原始数据”填入二维variables数组中,循环遍历每个级别,根据具体情况构build父级或次级子级的子级。

 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 

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

变体数组解析子项