将UDT数组的特定元素传递给函数

时间:2014-07-02 20:22:44

标签: vba excel-vba excel

所以我有一个这样的UDT设置:

Public Type UserInfo
name as string
username as string
active_time as double
End Type
然后我创建了一个这种类型的数组:

Dim list_of_users() as UserInfo
'Populate array here

我想要做的是将active_time值作为数组传递给单独的函数。类似的东西:

'StdDev function defined elsewhere    
standard_dev_all = StdDev(list_of_users().active_time) 

这甚至可能吗?我想我可以修改函数来处理我的UDT,但是我有更多的值而不仅仅是active_time,看起来这会让它变得非常混乱。

1 个答案:

答案 0 :(得分:1)

您不能将其作为

传递
UDTvariable.Element
' or 
UDTvariable().Element

成功能。我的意思是,

  1. 第一个无效,因为没有指定数组元素的索引(不是元素的元素)。
  2. 第二个也无效。
  3. 解决方案是传递给你的功能

    Answer = Stdev(UDTVariable)
    

    并在函数内部执行此操作

    Function Stdev(ByRef UDTVariableTypeName UDTVariable)
        for i = 1 to N
            Something = UDTVariable(i).Element
            ' so on and so forth
        next i
    
        Stdev = SomeAnswer
    End Function
    

    你可以省略写ByRef,因为这是传递参数的默认方式,但我为了清楚起见保留了它。

相关问题