UDF returns the same value everywhere

杀马特。学长 韩版系。学妹 提交于 2019-11-28 01:34:41

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 resides on the worksheet, use the Application.Caller method. The Range.Parent property can be used to explicitly identify the worksheet (and avoid further confusion) in a With ... End With statement.

Function trial1(a As Integer) As Variant

    Application.Volatile
    Dim rng As Range
    with Application.Caller.Parent
        Set rng = .Range(.Cells(Application.Caller.Row, 2), _
                         .Cells(Application.Caller.Row - a + 1, 2))
        trial1 = (Application.Sum(rng)) * (1 / a)
    end with

End Function

You've applied the Application.Volatile¹ method but allowed the range to be averaged to default to the ActiveSheet property by not explcitly specifying the parent worksheet.

The average is computed with the Excel Application object returning a SUM function's result and some maths. The same could have been returned in one command with the worksheet's AVERAGE function but blank cells would be handled differently.

        trial1 = Application.Average(rng)

¹ Volatile functions recalculate whenever anything in the entire workbook changes, not just when something that affects their outcome changes.

It's kind of strange to me for a UDF to calculate moving average given a number. If this UDF is to be used within the Worksheet, I believe you would put it next to existing data and if you want to change the size of the range for average amount, you update them manually?

Assuming you can name a Range "MovingAverageSize" to store the size of the range to calculate the average, and the average amount on the right of the existing data, consider below:

  • Range C2 is named MovingAverageSize
  • Data stored from B3 and downwards
  • Moving Average result is stored 1 column on the right of the data
  • If the data is less than MovingAverageSize, the SUM function adjusts accordingly
  • Any calculation error occurs with result in zero
  • Every time MovingAverageSize changes value, it triggers a Sub to update the formulas (Codes are placed in the Worksheet object rather than normal Module)
  • Alternatively, you can change the code to place the MovingAverage to same column of the MovingAverageSize, so you can have a few different size comparing next to each other.

Code in Worksheet Object:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 Then
        If Target.Address = ThisWorkbook.Names("MovingAverageSize").RefersToRange.Address Then UpdateMovingAverage Target
    End If
End Sub

Private Sub UpdateMovingAverage(ByRef Target As Range)
    Dim oRngData As Range, oRng As Range, lSize As Long, lStartRow As Long
    Debug.Print "UpdateMovingAverage(" & Target.Address & ")"
    If IsNumeric(Target) Then
        lSize = CLng(Target.Value)
        If lSize <= 0 Then
            MsgBox "Moving Average Window Size cannot be zero or less!", vbExclamation + vbOKOnly
        Else
            ' Top Data range is "B3"
            Set oRngData = Target.Parent.Cells(3, "B") ' <-- Change to match your top data cell
            lStartRow = oRngData.Row
            ' Set the Range to last row on the same column
            Set oRngData = Range(oRngData, Cells(Rows.Count, oRngData.Column).End(xlUp))
            Application.EnableEvents = False
            For Each oRng In oRngData
                If (oRng.Row - lSize) < lStartRow Then
                    oRng.Offset(0, 1).FormulaR1C1 = "=iferror(sum(R[" & lStartRow - oRng.Row & "]C[-1]:RC[-1])/MovingAverageSize,0)"
                Else
                    oRng.Offset(0, 1).FormulaR1C1 = "=iferror(sum(R[" & 1 - lSize & "]C[-1]:RC[-1])/MovingAverageSize,0)"
                End If
            Next
            Application.EnableEvents = True
            Set oRngData = Nothing
        End If
    End If
End Sub

Sample data and screenshots


I believe that Application.ActiveCell is not what you should be using here. Application.ThisCell would be more appropriate assuming that "a" is the size of the subset and that the dataset is 1 column on the right. Moreover, I would simply use "WorksheetFunction.Average" instead of "Application.Sum" and I would add "Application.Volatile" so the average is recalculated whenever an update occurs on the worksheet.

So one solution to your issue would be:

Public Function Trial1(a As Integer) As Variant
  Application.Volatile
  Trial1 = WorksheetFunction.Average(Application.ThisCell(1, 2).Resize(a))
End Function

Another solution here would be to use an array formula entered with Control/Shift/Enter:

Public Function MovAvg(dataset As Range, subsetSize As Integer)
  Dim result(), subset As Range, i As Long
  ReDim result(1 To dataset.Rows.count, 1 To 1)
  Set subset = dataset.Resize(subsetSize)

  For i = 1 To dataset.Rows.count
    result(i, 1) = WorksheetFunction.Average(subset.offset(i - 1))
  Next

  MovAvg = result
End Function

And to use this array function:

  • Select the range where all the results will be written (should be the size of your dataset)
  • Type "=MovAvg(A1:A100, 2)" where A1:A100 is the source of the data and 2 the size of the subset
  • Press Ctrl+Shift+Enter

A UDF should only access a range when it is passed as a parameter. Also, you should eliminate Application.Volatile because (1) your calculation is deterministic and not volatile, (2) Excel will re-calculate automatically your UDF whenever any cell in the input range changes, and (3) because the 'volatile' attribute in a UDF can make a model very slow so it should avoided when not necessary. So, for a moving average, the correct formula is:

Public Function SpecialMovingAverage(Rng as Excel.Range) As Double
    Dim denominator as Integer
    denominator = Rng.Cells.Count
    if Denominator = 0 then SpecialMovingAverage = 0: exit function 
    ' write your special moving average logic below
    SpecialMovingAverage = WorksheetFunction.Average(Rng) 
End Function

Note: I changed the answer following two comments because I initially did not see that the question was after a moving average (maybe the question was changed after my answer, or I initially missed the UDF's stated objective).

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!