简体   繁体   English

带有 UDF 的多个输出单元

[英]Multiple output cells with UDF

I want to print multiple output fields in cells in Excel using a user defined function.我想使用用户定义的函数在 Excel 的单元格中打印多个输出字段。

Function CAL(dinero, impuesto)
Dim strText As String
Dim num As Double

num = dinero * 6.8
strText = "Mas impuesto"

Range("A2").Value = strText
Range("B2").Value = num

CAL = dinero + impuesto
CAL = Application.Round(CAL, 2)
End Function

Current result:当前结果:

在此处输入图片说明

Expected result:预期结果:

在此处输入图片说明

You could do that with a Macro.你可以用宏来做到这一点。

With a Function, you could output an array.使用函数,您可以输出一个数组。 But the cells would be contiguous and could not be specifically within the function.但是这些单元格将是连续的,并且不能具体地位于函数内。

For example, with your formula in A1例如,使用A1的公式

Function CAL(dinero, impuesto)
Dim strText As String
Dim num As Double
Dim calc As Double
Dim v(1 To 2, 1 To 2) As Variant

num = dinero * 6.8
strText = "Mas impuesto"

calc = dinero + impuesto
calc = Application.Round(calc, 2)

v(1, 1) = calc
v(1, 2) = ""
v(2, 1) = strText
v(2, 2) = num

CAL = v
End Function

在此处输入图片说明

If your version of Excel does not have dynamic arrays, you can enter the formula as an array across the four cells;如果您的 Excel 版本没有动态数组,您可以将公式作为跨四个单元格的数组输入; or enter four formulas using INDEX to return each individual component.或使用INDEX输入四个公式以返回每个单独的组件。

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

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