简体   繁体   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?

Suppose I have 2 class modules named 'myFruits':假设我有 2 个名为“myFruits”的 class 模块:

Public nApples As Integer
Public nOranges As Integer

and 'fruitPrice':和“水果价格”:

Public Apple As Double
Public Orange As Double

I want to write 3 functions/modules 'showFruits', 'showPrice' and 'getValue' which I can call in excel worksheet, with inputs and outputs below, and able to display/print in the excel sheet in this manner: https://imgur.com/a/fGNoHCz我想编写 3 个函数/模块“showFruits”、“showPrice”和“getValue”,我可以在 excel 工作表中调用它们,输入和输出如下,并能够以这种方式在 excel 工作表中显示/打印: https:/ /imgur.com/a/fGNoHCz

The first two functions' outputs (class objects) will be inputs for the third function, 'getValue', which returns the value of number of fruits * fruit price.前两个函数的输出(类对象)将作为第三个 function“getValue”的输入,它返回水果数量 * 水果价格的值。

I'm familiar with Python but new to VBA, and I'm having trouble finding resources that do exactly this.我对 Python 很熟悉,但对 VBA 还是陌生的,而且我很难找到完全可以做到这一点的资源。 Any help is appreciated!任何帮助表示赞赏!

This is my attempt so far:到目前为止,这是我的尝试:

1. 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

A lot going on here, but I'm not sure I understand why you're using a Class Module at all.这里发生了很多事情,但我不确定我是否完全理解您为什么使用 Class 模块。

For the function piece, check out documentation on Creating custom functions in Excel .对于 function 部分,请查看有关在 Excel 中创建自定义函数的文档 I've always called these User Defined Functions , so you might find some luck searching for that as well.我一直称这些为User Defined Functions ,因此您也可能会找到一些运气来搜索它。

Something like this to get started:像这样开始:

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