0

シリーズの長方形の丸めを実行する VBA プラグイン関数を作成します。私の VBA メソッドでは、数式/VBA メソッドを含むセルの上に空のセルがあるかどうかを検出したいと思います。しかし、メソッドで ActiveCell を使用すると、Excel は循環参照を訴え、メソッドの戻り値ではなく 0 を返します。方法の例:

Function MovingAverageSmooth(r As Range, m As Integer)
    ' returns a smoothed average using the 'rectangular' method
    Dim cStart As Long, x As Long, total As Double, activeColumn As Long
    Dim vc As Long, vr As Long, count As Double, beforeCount As Long, afterCount As Long

    vc = r.Column
    vr = r.Row

    rStart = Max(1, vr - m)
    currentValue = Cells(vr, vc).Value
    activeColumn = ActiveCell.Column
    For x = rStart To vr + m
        If Application.IsNumber(Cells(x, vc).Value) Then
            total = total + Cells(x, vc).Value
            count = count + 1
            If Application.IsNumber(Cells(x, activeColumn).Value) Then
                If x < vr Then
                    beforeCount = beforeCount + 1
                End If
                If x > vr Then
                    afterCount = afterCount + 1
                End If
            End If
        End If
    Next
    MovingAverageSmooth = total / count
    If afterCount = 0 Or beforeCount = 0 Or count = 0 Then
        MovingAverageSmooth = currentValue
    End If

End Function
4

1 に答える 1

1

これでうまくいくと思います。私のコメントで述べたように、Application.Caller は関数を呼び出したセルを返します。

Function MovingAverageSmooth(r As Range, m As Integer)
    ' returns a smoothed average using the 'rectangular' method
    Dim cStart As Long, x As Long, total As Double, activeColumn As Long
    Dim vc As Long, vr As Long, count As Double, beforeCount As Long, afterCount As Long

    vc = r.Column
    vr = r.Row

    rStart = Max(1, vr - m)
    currentValue = Cells(vr, vc).Value
    activeColumn = Application.Caller.Column
    For x = rStart To vr + m
        If Application.IsNumber(Cells(x, vc).Value) Then
            total = total + Cells(x, vc).Value
            count = count + 1
            If Application.IsNumber(Cells(x, activeColumn).Value) Then
                If x < vr Then
                    beforeCount = beforeCount + 1
                End If
                If x > vr Then
                    afterCount = afterCount + 1
                End If
            End If
        End If
    Next
    MovingAverageSmooth = total / count
    If afterCount = 0 Or beforeCount = 0 Or count = 0 Then
        MovingAverageSmooth = currentValue
    End If
End Function
于 2012-09-12T05:05:01.890 に答える