添加计数器到ElseIF语句

时间:2015-08-07 00:47:22

标签: excel-vba if-statement counter vba excel

我有以下代码检查四种不同的条件,并给出满足每种条件的单元格的累积值。我想添加一个计数器,只计算满足特定条件的单元格。下面,我有市场与k的柜台。例如,如果UpDown = 1,我希望计数器只计算满足该条件的单元格数。目前,我的代码不起作用。任何帮助将不胜感激!

Public Function Test(UpDown As Double, rng_port As Range, rng_bench As Range) As Double

    Dim l As Long, temp As Double, k As Integer

    k = 0

    If rng_port.Count <> rng_bench.Count Then upcaptureratio = xlErrValue

    cumret_port = 1

    For l = 1 To rng_bench.Count
        If UpDown = 1 Then
            temp = 1 + IIf(rng_bench.Cells(l) >= 0.0025, rng_port.Cells(l), 0)
            k = k + 1
        ElseIf UpDown = 2 Then
            temp = 1 + IIf(rng_bench.Cells(l) < 0.0025 And rng_bench.Cells(l) >= 0, rng_port.Cells(l), 0)
            k = k + 1
        ElseIf UpDown = 3 Then
            temp = 1 + IIf(rng_bench.Cells(l) < 0 And rng_bench.Cells(l) >= -0.0025, rng_port.Cells(l), 0)
            k = k + 1
        ElseIf UpDown = 4 Then
            temp = 1 + IIf(rng_bench.Cells(l) < -0.0025, rng_port.Cells(l), 0)
            k = k + 1
        End If

        cumret_port = cumret_port * temp

    Next

    Test = cumret_port^(1 / k) - 1

0 个答案:

没有答案