繁体   English   中英

VBA 如何将 output class 模块(对象)从 function 转换为 excel 工作表,并使用该 output 作为另一个 function 的输入?

[英]VBA how to output class module (object) from function to excel worksheet, and using that output as input for another function?

假设我有 2 个名为“myFruits”的 class 模块:

Public nApples As Integer
Public nOranges As Integer

和“水果价格”:

Public Apple As Double
Public Orange As Double

我想编写 3 个函数/模块“showFruits”、“showPrice”和“getValue”,我可以在 excel 工作表中调用它们,输入和输出如下,并能够以这种方式在 excel 工作表中显示/打印: https:/ /imgur.com/a/fGNoHCz

前两个函数的输出(类对象)将作为第三个 function“getValue”的输入,它返回水果数量 * 水果价格的值。

我对 Python 很熟悉,但对 VBA 还是陌生的,而且我很难找到完全可以做到这一点的资源。 任何帮助表示赞赏!

到目前为止,这是我的尝试:

1.

Function showFruits(nApples As Integer, nOranges As Integer) As myFruits

    Set showFruits = New myFruits

    showFruits.nApples = nApples
    showFruits.nOranges = nOranges

    'How to print above variables in excel?
    'How to use the output object of this function as input for another function in excel?

End Function
Function showPrice(pApple As Double, pOrange As Double) As fruitPrice

    Set showPrice = New fruitPrice

    showPrice.Apple = pApples
    showPrice.Orange = pOranges

    'How to print above variables in excel?
    'How to use the output object of this function as input for another function in excel?

End Function
Function getValue(showFruits As myFruit, showPrice As fruitPrice)

    'How to pass the output objects as input to this function in excel?

    Dim total As Double
    total = showFruits.nApples * showPrice.Apple + showFruits.nOranges * showPrice.Orange
    getValue = total

End Function

这里发生了很多事情,但我不确定我是否完全理解您为什么使用 Class 模块。

对于 function 部分,请查看有关在 Excel 中创建自定义函数的文档 我一直称这些为User Defined Functions ,因此您也可能会找到一些运气来搜索它。

像这样开始:

Function showFruits(nApples) As String

'How to print above variables in excel?
'Simply assign a value/output/something to your function

showFruits = "Apples: " & nApples

End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM