调用从电子表格返回自定义types的VBA函数

我有一个vba函数返回一个自定义数据types,定义为:

Public Type stockValue stock As String value As Double End Type 

我的问题是,当我从电子表格单元格调用函数时,如何处理这个问题? 例如,说我想单元格显示stock价值,我试过=function().stock ,它不工作

任何帮助表示赞赏,谢谢!

 Function getLowestPnl(strat As String, rank As Integer) As stockValue Call Conecta_DB(conexao) Set registros = New ADODB.Recordset strSQL = "SELECT stock,sum([value]) FROM Reports.dbo.Entry WHERE idStrategy='" & strat & "' and idType=1 GROUP BY stock ORDER BY sum([value])" 'strSQL = "SELECT [finance],[sales],[management],[research],[administration] FROM [COS].[dbo].[Complementarity] WHERE [idCompany] =" & idCompany & " and [year]=" & year & " and [CEO]=1" registros.Open strSQL, conexao, adOpenStatic, adLockOptimistic parar = False If Not registros.EOF Then x = registros.GetRows() i = 0 Do While parar <> True If i = (rank - 1) Then getLargestShortExp.stock = Trim(x(0, i)) getLargestShortExp.value = x(1, i) parar = True End If i = i + 1 Loop End If registros.Close getLowestPnl = ret End Function 

您只能返回Excel从用户定义的函数中理解的数据types。 Excel不理解自定义数据types。

相反,您将不得不返回一个包含您的自定义数据types的2个值的变体数组。 然后要么数组input到2个单元格中的函数,要么使用另一个函数,如INDEX从返回的数组中检索您想要的值。