清空 VBA 集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16842751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making empty a VBA collection
提问by Antonio E.
I have a problem with a Collection object in Visual Basic for Aplications (I use it with Excel)
我在 Visual Basic for Applications 中遇到一个 Collection 对象问题(我在 Excel 中使用它)
I have this code trying to empty a Collection object that I have to re-use:
我有这段代码试图清空一个我必须重新使用的 Collection 对象:
Sub limpieza(ByRef listilla As Collection)
While listilla.Count <> 0
listilla.Remove (listilla.Count)
Wend
End Sub
But when I call it with:
但是当我用以下方式调用它时:
Dim listado As New Collection
<I have some code here that add some values to the collection>
limpieza (listado)
VBA says to me that
VBA 对我说
argument is not optional
参数不是可选的
and the code doesn't run.
并且代码不运行。
What can I do? I need to use this collection cleaning at the bottom of a loop that reuses the Collection object.
我能做什么?我需要在重复使用 Collection 对象的循环底部使用此集合清理。
回答by Dick Kusleika
If you want to empty a collection, instead of calling a separate procedure, just use
如果你想清空一个集合,而不是调用一个单独的过程,只需使用
Set listado = New Collection
回答by enderland
Dim listado As New Collection
<I have some code here that add some values to the collection>
limpieza listado
'this syntax also works
Call limpieza(listado)
Note that I removed the ()
around the argument.
请注意,我删除()
了参数的周围。
When passing byref
you want to do this as ()
causes VBA to pass it as byval
by default unless you add Call
as well.
传递时,byref
您希望这样做,因为()
VBA 将byval
默认传递它,除非您也添加Call
。
This can be really frustrating since you can often actually use subName(args)
as syntax sometimes but run into these sorts of problems. I generally use Call mySubName(args)
to make things more clear.
这真的很令人沮丧,因为subName(args)
有时您实际上可以实际使用as 语法,但会遇到此类问题。我通常Call mySubName(args)
用来使事情更清楚。