0 长度 Iterable 与 VBA 中的 For Each 循环兼容

问题描述 投票:0回答:1

对我来说,在 Excel 的 VBA 中有一个 For Each 循环,这看起来确实令人难以置信,但我根本无法创建循环迭代 0 次的任何类型的数据(因此循环的核心永远不会执行)。

Function GetChildren() As Variant()
  Dim children()
  GetChildren = children
End Function

For Each something In GetChildren()
  ' Do something
Next something

有什么吗

  • 与 For Each 结构兼容
  • 无需检查(错误、空虚等)客户端应该很简单
  • 在 GetChildren 中我可以做任何事来交换

所以我想我正在寻找一个可以循环 0 次的数据结构来返回。

提前致谢

vba loops
1个回答
0
投票

使用

Collection
对象有 0 次迭代。

Function GetChildren() As Object

  Set childrens = New Collection
  Set GetChildren = childrens
  
End Function

Sub caller()

For Each something In GetChildren
  Debug.Print TypeName(something)
Next something

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.