Excel公式显示最接近的非空单元格的值?

Excel Formula to show the value of the nearest non-empty above cell?

提问人:MistyBreeze 提问时间:10/25/2023 最后编辑:MistyBreeze 更新时间:10/28/2023 访问量:74

问:

需要一个 excel 公式来显示最接近的非空单元格的值。在某些情况下,直接引用;在其他情况下,值 + 1。

这是我的尝试,但是,Cell.End() 是一个 VBA 函数,因此它仍然不是动态的,因为表的扩展无法继承该模式。

Const CELL_FML_L As String = "=If(Indirect(""A""&Row()-1)=""#"",1,Indirect(""A""&"

Const CELL_FML_R As String = ")+1)"

'在“对于[表]中的每个单元格”循环中

Cell.FormulaR1C1 = CELL_FML_L & Cell.End(xlUp).Row & CELL_FML_R

有没有办法完全用excel公式实现这一点?

非常感谢您的帮助!见解也会有所帮助。

Excel VBA 用户界面 excel-formula

评论

1赞 Nick Abbot 10/26/2023
什么?这是在哪里定义的?...For 循环的一部分?CELL_FML_R
0赞 MistyBreeze 10/28/2023
我的错。它们是分块写的。
0赞 user3598756 10/29/2023
您可能想分享一些有关您要实现的目标的更多信息/示例

答:

1赞 Darren Bartrup-Cook 10/26/2023 #1

当您使用 VBA 标记时,也许是 UDF:

Public Function FindPrevious(Target As Range) As Variant

    'Application.Volatile  'Uncomment to make function volatile -
                           'Recalculates whenever calculation occurs in any cells on the worksheet

    If Target.Cells.Count > 1 Then
        FindPrevious = CVErr(xlErrRef) 'Return #REF! error.
    Else
        Dim rTmp As Range
        Set rTmp = Target.EntireColumn.Find(What:="*", After:=Target, LookIn:=xlValues, LookAt:=xlWhole, _
                                      SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        If Not rTmp Is Nothing Then
            If rTmp.Row < Target.Row Then
                FindPrevious = rTmp.Value
            Else
                FindPrevious = CVErr(xlErrNull) 'Return #NULL! error
            End If
        End If
    End If

End Function  

enter image description here

评论

0赞 MistyBreeze 10/28/2023
永远不知道可以应用观察者模式而不是“Worksheet_SelectionChange”。我的朋友推荐了'COUNTA()'和'INDIRECT()'&'Row()'。谢谢!如果解决,将更新。