Call VBA function that returns custom type from spreadsheet

风流意气都作罢 提交于 2019-12-01 23:39:07

You can only return data types that Excel understands from a user-defined function. Excel does not understand custom data types.

Instead you would have to return a variant array containing the 2 values from your custom data type. Then either you would array enter the function into 2 cells, or use another function such as INDEX to retrieve the value you wanted from the returned array.

You can do the following :

Type XYDouble
    X As Double
    Y As Double
End Type

Private Function Compute(ByVal X As Double, ByVal Y As Double) As XYDouble
    Compute.X = ....
    Compute.Y = ...
End Function


Function ComputeX(ByVal X As Double, ByVal Y As Double) As Double
    ComputeX = Compute(X, Y).X
End Function

Function ComputeY(ByVal X As Double, ByVal Y As Double) As Double
    ComputeY = Compute(X, Y).Y
End Function

and, you can get the X (or Y) value in cell with the formula : "=ComputeX(C7;D7)"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!