excel - 如何在 VBA 中的另一个函数中调用一个函数?

标签 excel vba

我试图在另一个函数中调用一个函数,但我得到的结果是 0
enter image description here

Function DepositRate(Deposit):
    If Deposit > 100000 Then
        Rtaxa = 0.078
    ElseIf Deposit <= 100000 And Deposit > 10000 Then
        Rtaxa = 0.073
    ElseIf Deposit <= 10000 And Deposit > 1000 Then
        Rtaxa = 0.063
    ElseIf Deposit <= 1000 And Deposit > 0 Then
        Rtaxa = 0.055
    Else
        Rtaxa = "Negative Value"
    End If
    
    DepositRate = Rtaxa
        
End Function

Function NewDFV(Value, Year):
    Call DepositRate(Value)
    ValorFuturo = Deposit * (1 + Rtaxa) ^ (Year)
    NewDFV = ValorFuturo
    
End Function 
那么,我在这里做错了吗?

最佳答案

函数调用函数

  • 请注意,这两个函数都可以用作 UDF。如果您不希望第一个函数出现在函数列表中,请使用 Private Function GetDepositrate(...)...同时将两个功能保留在同一个模块中。该功能仍然可用,只是未列出。

  • Option Explicit
    
    Function GetDepositrate(ByVal Deposit As Double) As Double
        
        Dim RTaxa As Double
        
        If Deposit > 100000 Then
            RTaxa = 0.078
        ElseIf Deposit > 10000 Then
            RTaxa = 0.073
        ElseIf Deposit > 1000 Then
            RTaxa = 0.063
        ElseIf Deposit > 0 Then
            RTaxa = 0.055
        Else
            ' Negative Value - do nothing
        End If
        
        GetDepositrate = RTaxa
    
    End Function
    
    Function NewDFV(ByVal Deposit As Double, ByVal Year As Long) As Double
        
        Dim RTaxa As Double: RTaxa = GetDepositrate(Deposit)
        
        NewDFV = Deposit * (1 + RTaxa) ^ (Year)
    
    End Function
    

    关于excel - 如何在 VBA 中的另一个函数中调用一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71058457/

    相关文章:

    python - xl.Workbook() (pyvot) 无法打开 Excel 工作簿

    Excel公式: If condition then do that condition

    c# - 读取Excel文件

    excel - 打开时自动运行宏的好方法是什么

    excel - 无法获取 Worksheet 类的 Copy 属性

    vba - 根据另一个单元格值将公式写入单元格

    vba - 根据输入生成数据库中多个单元的排列

    vba - 在排序中从日期时间中剥离时间

    excel - VBA 函数和变量

    excel - 为什么Excel中的这个循环会出现 "Out of Memory"错误?