获取父细胞参考/传递特性

我有一个问题,我需要引用父单元格。 所以说A1的公式是“= B1”,B1的公式是“= C1”。 我需要把A1的公式压缩到“= C1”。 我不确定是否有办法在Excel中做到这一点,或者如果有办法做到这一点的Apache POI。 我环顾四周,但似乎无法find解决办法。 有没有人知道如何在Excel或POI API做到这一点?

在你的Sample运行中,这会给你你所要求的结果。

 Sub GetLastPrecedent() Dim pres As Range Dim TestCell As Range Set TestCell = Range("A1") 'Set pres to all cells that are used in getting the value of TestCell 'This includes all precedents of precedents Set pres = TestCell.Precedents 'This will return the absolute precedent of the ones returned Set pres = pres.Cells(pres.Rows.Count, pres.Columns.Count) 'This will set the formula in TestCell to use the absolute address TestCell.Formula = "=" & pres.Address End Sub 

我希望这至less能帮助你引导你寻找什么。 更多信息将导致更好的答案。 记住,如果你有复杂的公式,参考许多单元格,这将变得非常危险和复杂。 我只根据所提供的信息提供这个样本来帮助指导你。