简体   繁体   中英

Multiple output cells with UDF

I want to print multiple output fields in cells in Excel using a user defined function.

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

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; or enter four formulas using INDEX to return each individual component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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