Excel VBA加权平均值如果自定义函数

时间:2013-12-02 19:04:54

标签: excel excel-vba excel-formula vba

我正在尝试编写一个自定义函数,它将检查某个范围内的2个条件,然后对它找到的任何匹配项进行加权平均。我的代码如下,并没有正确解释我的标准。它在我的标准上显示#Value。任何帮助将不胜感激!谢谢:D

https://drive.google.com/file/d/0B6aefGNRPaHiRlR4a3Vvc3RNamM/edit?usp=sharing

Function WeightedAverageIf(Range As Range, Citeria1 As String, Column1 As Integer, Criteria2 As String, Column2 As Integer, Column3 As Integer, Column4 As Integer)
     lr = Range.Cells(Rows.Count, 1).End(xlUp).Row
     Dim num1 As Integer, per1 As Integer, num2 As Integer, per2 As Integer, counter As Integer
     coutner = 0
     For x = 1 To lr
        If counter = 0 Then
           If Range.Cells(x, Column1) = Criteria1 And Range.Cells(x, Column2) = Criteria2 Then num1 = Cells(x, Column3) And per1 = Range.Cells(x, Column4) And counter = counter + 1
           End If
        If counter > 0 Then
           If Range.Cells(x, Column1) = Criteria1 And Cells(x, Column2) = Criteria2 Then num2 = Range.Cells(x, Column3) And per2 = Range.Cells(x, Column4) And counter = counter + 1
           per1 = (((per1 * num1) + (per2 * num2)) / (num1 + num2))
           num1 = (num1 + num2)
        End If
     Next x
     WeightedAverageIf = per1
End Function

1 个答案:

答案 0 :(得分:1)

感谢您的帮助,您指出了我正确的方向!我不得不添加一些检查并移动if语句,但我有一个适用于任何人的最终产品,所以我决定发布完成的代码。

以下是只有一个匹配条件的代码。

Function WeightedAverageIf(Rng As Range, Find1 As String, Find1Column As Integer, WeightColumn As Integer, AVGColumn As Integer)
    lr = Rng.Rows.Count
    Dim num1 As Double, per1 As Double, num2 As Double, per2 As Double, counter As Integer
    coutner = 0

    For x = 1 To lr

        If counter > 0 Then
            If Rng.Cells(x, WeightColumn) = 0 Or Rng.Cells(x, WeightColumn) = "" Or IsNumeric(Rng.Cells(x, AVGColumn)) = False Or IsNumeric(Rng.Cells(x, WeightColumn)) = False Then GoTo skipif
            If Rng.Cells(x, Find1Column) = Find1 Then
                num2 = Rng.Cells(x, WeightColumn)
                per2 = Rng.Cells(x, AVGColumn)
                counter = counter + 1
            End If

            per1 = (((per1 * num1) + (per2 * num2)) / (num1 + num2))
            num1 = (num1 + num2)
        End If

        If counter = 0 Then
            If Rng.Cells(x, WeightColumn) = 0 Or Rng.Cells(x, WeightColumn) = "" Or IsNumeric(Rng.Cells(x, AVGColumn)) = False Or IsNumeric(Rng.Cells(x, WeightColumn)) = False Then GoTo skipif
            If Rng.Cells(x, Find1Column) = Find1 Then
                num1 = Rng.Cells(x, WeightColumn)
                per1 = Rng.Cells(x, AVGColumn)
                counter = counter + 1
            End If
        End If
skipif:
    Next x
    If counter = 1 Then
    WeightedAverageIf = per1
    ElseIf counter = 0 Then
    WeightedAverageIf = 0
    Else
    WeightedAverageIf = per1
    End If
End Function

以下是具有2个匹配条件的代码。

Function WeightedAverageIfs(Rng As Range, Find1 As String, Find1Column As Integer, Find2 As String, FindColumn2 As Integer, WeightColumn As Integer, AVGColumn As Integer)
    lr = Rng.Cells(Rows.Count, 1).End(xlUp).Row
    Dim num1 As Double, per1 As Double, num2 As Double, per2 As Double, counter As Integer
    coutner = 0

    For x = 1 To lr

        If counter > 0 Then
            If Rng.Cells(x, WeightColumn) = 0 Or Rng.Cells(x, WeightColumn) = "" Or IsNumeric(Rng.Cells(x, AVGColumn)) = False Or IsNumeric(Rng.Cells(x, WeightColumn)) = False Then GoTo skipif
            If Rng.Cells(x, Find1Column) = Find1 And Rng.Cells(x, FindColumn2) = Find2 Then
                num2 = Rng.Cells(x, WeightColumn)
                per2 = Rng.Cells(x, AVGColumn)
                counter = counter + 1
            End If

            per1 = (((per1 * num1) + (per2 * num2)) / (num1 + num2))
            num1 = (num1 + num2)
        End If

        If counter = 0 Then
            If Rng.Cells(x, WeightColumn) = 0 Or Rng.Cells(x, WeightColumn) = "" Or IsNumeric(Rng.Cells(x, AVGColumn)) = False Or IsNumeric(Rng.Cells(x, WeightColumn)) = False Then GoTo skipif
            If Rng.Cells(x, Find1Column) = Find1 And Rng.Cells(x, FindColumn2) = Find2 Then
                num1 = Rng.Cells(x, WeightColumn)
                per1 = Rng.Cells(x, AVGColumn)
                counter = counter + 1
            End If
        End If
skipif:
    Next x
    If counter = 1 Then
    WeightedAverageIfs = per1
    ElseIf counter = 0 Then
    WeightedAverageIfs = 0
    Else
    WeightedAverageIfs = per1
    End If
End Function

我希望这将有助于将来的人们。对于不熟悉excel和VBA的人,您需要打开开发人员控制台。插入一个新模块并粘贴上面的一个代码段。执行此操作后,您只需键入带参数= WeightedAverageIf的函数(“数据范围”,“您需要在范围内匹配的内容”,“范围中的哪一列是”中的搜索数据,“”带加权的列平均数字“,”具有平均数字的列“)