UDF returns the same value everywhere
问题 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.Volatile Dim rng As Range Set rng = Range(Cells(ActiveCell.Row, 2), Cells(ActiveCell.Row - a + 1, 2)) trial1 = (Application.Sum(rng)) * (1 / a) End Function 回答1: The ActiveCell property does not belong in a UDF because it changes . Sometimes, it is not even on the same worksheet. If you need to refer to the cell in which the custom UDF function