功能不起作用 - 显示0

时间:2015-07-27 14:14:17

标签: excel vba excel-vba

我写了一个简单的函数来计算公司奖金。由于某种原因,它显示0并且没有给出答案。

Function insurancebonus(penetration As Single, penetrationPlan As Single, _ 
    renewalRate As Single, renewalPlan As Single, dealerRank As Single, _ 
    premiumDynamic As Single) As Single
    Dim Total As Single
    Total = 0
    If penetration >= penetrationPlan Then Total = Total + 0.01
    If renewalRate >= renewalPlan Then Total = Total + 0.01
    Select Case dealerRank
    Case 1
        Select Case premiumDynamic
            Case 0 To 0.03: Total = Total + 0.01
            Case 0.03 To 0.05: Total = Total + 0.02
            Case 0.05 To 1: Total = Total + 0.03
        End Select
    Case 2
        Select Case premiumDynamic
            Case 0.02 To 0.04: Total = Total + 0.01
            Case 0.04 To 0.08: Total = Total + 0.02
            Case 0.08 To 1: Total = Total + 0.03
        End Select
    Case 3
        Select Case premiumDynamic
            Case 0.05 To 0.1: Total = Total + 0.01
            Case 0.1 To 0.15: Total = Total + 0.02
            Case 0.15 To 1: Total = Total + 0.05
        End Select
    Case 4
        Select Case premiumDynamic
            Case 0.1 To 0.17: Total = Total + 0.01
            Case 0.17 To 0.25: Total = Total + 0.02
            Case 0.25 To 1: Total = Total + 0.03
        End Select
    Case 5
        Select Case premiumDynamic
            Case 0.15 To 0.3: Total = Total + 0.01
            Case 0.3 To 0.4: Total = Total + 0.02
            Case 0.4 To 1: Total = Total + 0.03
        End Select
    End Select
End Function

1 个答案:

答案 0 :(得分:8)

你还没有告诉函数返回任何内容。

我假设你想要返回Total的价值?

如果是这种情况,请在End Function之前添加以下行:

insurancebonus = Total

这指定您的函数应返回Total

的值

作为一个非常简短且人为的例子,这个函数只返回1。

Public Function test() As Integer
    test = 1
End Function

重点是您必须将值分配给您的函数名称。

为了完整性,如果您的函数返回Object类型,则必须使用Set关键字,例如:

Public Function testRange() As Range
    Set testRange = Range("A1")
End Function

哪会返回一个Range对象。