Excel 具有UDF的多个输出单元

Excel 具有UDF的多个输出单元,excel,vba,excel-formula,Excel,Vba,Excel Formula,我想使用用户定义的函数在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 + impu

我想使用用户定义的函数在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
当前结果:

预期结果:


您可以使用宏来实现这一点

使用函数,可以输出数组。但是,这些单元格是连续的,不能专门位于函数中

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
例如,在
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


如果您的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
例如,在
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


如果您的Excel版本没有动态数组,则可以在四个单元格中以数组形式输入公式;或者使用
INDEX
输入四个公式以返回每个单独的组件。

当使用从工作表单元格调用的VBA用户定义函数时,它只能返回一个值(或值数组)。它不能对工作表执行任何其他操作,例如将值写入其他单元格等。当使用从工作表单元格调用的VBA用户定义函数时,它只能返回值(或值数组)。它无法对工作表执行任何其他操作,例如将值写入其他单元格等。非常感谢!它适合我,我可以用arryas添加更多的代码。谢谢你的帮助,非常感谢!它适合我,我可以用arryas添加更多的代码。我感谢你的帮助。