VBA(绝对差值之和)范围

时间:2016-03-28 03:26:56

标签: vba excel-vba excel

编写一个带2个范围或2个数组作为输入的函数,如果这两个范围/数组具有相同数量的元素,则得到每个元素的绝对差值之和,如果这两个数组的大小,则向用户发送错误消息/范围不一样。这是一个VBA问题,我的时间很艰难。

这是我到目前为止所做的:

Public Function MyFunction(Rng1 As Range, Rng2 As Range)
    Dim CountRng1 As Long
    Dim CountRng2 As Long

    Rng1.Count = CountRng1
    Rng2.Count = CountRng2

    If CountRng1 = CountRng2 Then

    Else
        MsgBox "Error, the ranges you passed to the function are not the same size"
    End If
End Function

2 个答案:

答案 0 :(得分:0)

功能不显示任何弹出消息。因此,在您的代码中进行了轻微的更改以完成预期的任务。

Public Function MyFunction(Rng1 As Range, Rng2 As Range)
    Dim CountRng1 As Long
    Dim CountRng2 As Long

    CountRng1 = Rng1.Count
    CountRng2 = Rng2.Count

    If CountRng1 = CountRng2 Then
        'Your Code Goes here...
    Else
        MyFunction = "Error, the ranges you passed to the function are not the same size"
    End If
End Function

答案 1 :(得分:0)

您可以在单元格中使用简单公式来执行此操作:

=SUMPRODUCT(ABS(A1:A10-B1:B10))
相关问题