【问题标题】:VBA how to call function inside another function with partial argumentsVBA如何使用部分参数在另一个函数中调用函数
【发布时间】:2019-08-23 21:24:55
【问题描述】:

所以我想把下面的 Python 代码翻译成 VBA(只是玩具例子来说明问题,细节省略)。

def numerical_integration(fun, a, b):
    """

    :param fun: callable 
    :param a, b: float
    :return: float
    """
    # do something based on both fun and a, b
    res = ...
    return res


def h(x, k):
    """
    helper func
    :param x: float, main arg
    :param k: float, side param
    :return: float
    """
    # calc based on both x and k
    res = ...
    return res


def main_function(k):
    """

    :param k: float 
    :return: float
    """
    a, b = 0.0, 1.0
    res = numerical_integration(fun=lambda x: h(x, k), a=a, b=b)
    return res

问题是我不知道如何正确地将“部分”函数之类的东西作为 VBA 中的参数传递。我在this post 中找到了如何在 VBA 中传递“整个”函数。这个想法是将函数作为字符串传递,然后让 VBA 评估字符串(我猜类似于 Python eval 等价物?)。但是我不知道如果我的函数是部分的,这将有什么帮助,即我希望它只作为第一个参数的函数,例如 lambda x: f(x, k),其中第二个参数在主函数的上下文中。

提前感谢您的任何建议。

【问题讨论】:

标签: python excel vba


【解决方案1】:

我不知道 Python 和“部分”函数,但我尝试使用对象来替换它。 我创建了一个类模块,试图模仿“部分”函数,具有可选参数和默认值。

Public defaultX As Double
Public defaultY As Double

Public Function h(Optional x As Variant, Optional y As Variant) As Double

    ' The IsMissing() function below only works with the Variant datatype.

    If (IsMissing(x)) Then
        x = defaultX
    End If
    If (IsMissing(y)) Then
        y = defaultY
    End If

    h = x + y

End Function

然后我决定在 numeric_integration 中使用“CallByName”函数。 这是主要的模块代码:

Public Function numerical_integration(fun As clsWhatEver, funName As String, a As Double, b As Double) As Integer

    Dim r1 As Double, r2 As Double

    ' CallByName on the "fun" object, calling the "funName" function.
    ' Right after "vbMethod", the parameter is left empty
    ' (assuming default value for it was already set).
    r1 = CallByName(fun, funName, VbMethod, , a)
    r2 = CallByName(fun, funName, VbMethod, , b)

    numerical_integration = r1 + r2

End Function

Public Function main_function(k As Double) As Double
    Dim res As Double

    Dim a As Double, b As Double
    a = 0#
    b = 1#

    Dim oWE As New clsWhatEver
    ' Sets the default value for the first parameter of the "h" function.
    oWE.defaultX = k

    res = numerical_integration(oWE, "h", a, b)

    main_function = res

End Function

【讨论】:

    【解决方案2】:

    我真的不确定你在问什么,但如果你试图将第一个函数的结果作为第二个函数的参数传递,那么......

    Function numerical_integration(fun, a, b)
        numerical_integration = 1
    End Function
    
    Function h(x, k)
        h = 0
    End Function
    
    Sub main()
        Debug.Print numerical_integration(h(1, 2), 3, 4)
    End Sub
    

    (是的,这些都应该是强类型变量)

    【讨论】:

    • 不,不是结果。而是传递lambda x: h(x,k)mapping 本身。 numerical_integration 函数接受函数/映射作为输入,并返回整数值,即 f -> int f(x)dx from a to b
    • 我仍然不知道我是否理解了这个问题,但是您检查过CallByName 函数吗? docs.microsoft.com/en-us/office/vba/Language/Reference/…
    猜你喜欢
    • 2020-03-09
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多