【问题标题】:Return array of objects from function - VBA从函数返回对象数组 - VBA
【发布时间】:2018-01-25 20:19:52
【问题描述】:

我需要从我使用 VBA 创建的函数中“返回”一个对象数组。当我尝试将函数设置为数组时,它会给我一条错误消息,说

对象是必需的。

我不太习惯 VBA,我无法解决这个问题。函数代码如下:

Function sortedList(listRange As Integer, tempList() As ship) As ship
   Dim temp As ship
   Set temp = Nothing

   For i = listRange - 10 To 1 Step -1

       For j = 2 To listRange - 10
            If tempList(j - 1).Arrival > tempList(j).Arrival Then
                Set temp = tempList(j - 1)
                Set tempList(j - 1) = tempList(j)
                Set tempList(j) = temp
            End If
        Next j
    Next i

    'return tempList - how?
    Set sortedList = tempList

End Function

Ship 是我创建的“类”。 tempListship 类中的对象数组,我需要从函数 sortedList 返回。

函数可以工作,只是返回部分我不能工作。

感谢您的帮助。如果需要更多信息,请告诉我!

【问题讨论】:

  • 您是否尝试删除函数输出的类型规范?

标签: arrays vba excel function


【解决方案1】:

声明函数返回一个数组

Function sortedList(listRange As Integer, tempList() As ship) As ship()

然后将结果赋值为不带Set

sortedList = tempList

【讨论】:

  • 好的,成功了!现在,如何将我从该函数返回的内容分配给函数外部的变量?比如,Var1 = sortedList(param1, param2)
  • 就是这样。 :)
猜你喜欢
  • 2020-07-20
  • 2017-08-02
  • 2015-10-12
  • 1970-01-01
  • 2012-02-29
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多