UDF returns the same value everywhere

前端 未结 5 1905
闹比i
闹比i 2020-12-06 22:06

I am trying to code in moving average in vba but the following returns the same value everywhere.

Function trial1(a As Integer) As Variant
    Application.Vo         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 22:53

    I believe

    1. Your trial1() function is in one or more cells, as a part of a formula or by itself

    2. You want those cells to be recalculated whenever the user changes any cell on the worksheet

    For this, you'd need to identify the cell where the change happened. This cell is not given by

    A. ActiveCell - because that is the cell the cursor is on when the calculation starts; it could be anywhere but not on the cell that was changed

    B. Application.ThisCell - because that returns the cell in which the user-defined function is being called from, not the cell that changed

    The cell where the change happened is passed to the Worksheet's Change event. That event is triggered with an argument of type Range - the range that changed. You can use that argument to identify the cell(s) that changed and pass that to trial1(), possibly through a global variable (yeah, I know).

    I tried this in a worksheet and it works, so let me know your results.

提交回复
热议问题