如何使用VBA Excel

时间:2017-07-26 04:34:57

标签: vba

我的代码如下:

Function Depreciation(pCost As Currency, Age As Double)

    Dim cValue As Currency
    Dim Dep As Double

    Select Case Age
        Case Is < 1
            Dep = pCost
            cValue = pCost - Dep
            Depreciation = cValue

        Case Is < 2
            Dim cValue1 As Currency
            Depreciation = cValue * 0.25
            cValue1 = cValue - Depreciation
            Depreciation = cValue1

        Case Is < 3
            Dim cValue2 As Currency
            Depreciation = cValue1 * 0.25
            cValue2 = cValue1 - Depreciation
            Depreciation = cValue2
    End Select

End Function

1 个答案:

答案 0 :(得分:0)

假设您想要在取消后获得金额,请尝试以下方法:

Option Explicit

Function Depreciation(pCost As Currency, Age As Double)
    Const DepreciationRate As Currency = 0.25
    Dim cValue As Currency, i As Double

    cValue = pCost
    i = Age
    Do Until i < 1
        cValue = cValue * (1 - DepreciationRate)
        'Debug.Print Age - i + 1, cValue ' Uncomment to see value of wach year
        i = i - 1
    Loop
    Depreciation = cValue
End Function