如何recursion追加单元格?

我有一个如下所示的Excel工作表:

|ABCD ----------------------------------------------------------------------------------- 1 |ID PARENTID VALUE RESOLVED VALUE (how to generate this?) =================================================================================== 2 |0 0 /root /root 3 |1 0 /one /root/one 4 |2 0 /two /root/two 5 |3 0 /three /root/three 6 |4 3 /child-one-of-three /root/three/child-one-of-three 7 |5 3 /child-two-of-three /root/three/child-two-of-three 

每一行都有一个ID和一个PARENTID 。 我想要生成最后一列的内容,RESOLVED RESOLVED VALUE通过再次追加每行的VALUE

我怎样才能在Excel中做到这一点?

假设您的数据在单元格A3中开始:

 =IF(B4=A4,C4,VLOOKUP(B4,$A$3:$D$5,4)&C4) 

您必须将$A$3:$D$5扩展到您的数据数组的大小。

这是一个VBAfunction解决scheme。

 Option Explicit Public Function ResValue(id As Long, table As Range, indexID As Integer, indexParentID As Integer, indexValue As Integer) As String Dim strTmp As String, idTmp As Long idTmp = id ResValue = "" Do While idTmp <> 0 strTmp = Application.WorksheetFunction.Index(table.Columns(indexValue), Application.WorksheetFunction.Match(idTmp, table.Columns(indexID), 0)) ResValue = strTmp & ResValue idTmp = Application.WorksheetFunction.Index(table.Columns(indexParentID), Application.WorksheetFunction.Match(strTmp, table.Columns(indexValue), 0)) Loop ResValue = Application.WorksheetFunction.Index(table.Columns(indexValue), Application.WorksheetFunction.Match(0, table.Columns(indexID), 0)) & ResValue End Function 

您现在可以在工作表中使用ResValue(id, table, indexID, indexParentID, indexValue)来生成已parsing的值。

笔记:

  • id是您要为其生成parsing值的标识。
  • table是整个表的地址(不包括parsing值列)。
  • 每个索引参数是每个字段的相对列索引。 例如,ID在表的第一列中,所以indexID = 1,并且ParentID在第二列中,所以indexParentID = 2。

在您的示例表中,您将在D3 (解决值的第一个单元格)中input以下内容:

 =ResValue(A3,$A$3:$C$8,1,2,3) 

然后你可以填写这个公式。

在这里输入图像说明